forked from danielwaterworth/Raphters
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathrequest.c
153 lines (130 loc) · 3.98 KB
/
request.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/*
Copyright (C) 2011 Raphters authors,
This file is part of Raphters.
Raphters is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Raphters is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <uriparser/Uri.h>
#include <stdint.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <time.h>
#include "request.h"
#include "fcgi_stdio.h"
char *get_referer() {
return getenv("HTTP_REFERER");
}
char *get_user_agent() {
return getenv("HTTP_USER_AGENT");
}
char *get_path_info() {
return getenv("PATH_INFO");
}
char *get_query_string() {
return getenv("QUERY_STRING");
}
// note this method only returns data once for each request
// stdin cannot be rewinded
char *get_post_string() {
char *contentLength = get_content_length();
int len;
if (contentLength != NULL) {
len = strtol(contentLength, NULL, 10);
} else {
len = 0;
}
char *data = malloc((len+1) * sizeof(char));
FCGI_fgets(data, len+1, FCGI_stdin);
return data;
}
char *get_content_length() {
return getenv("CONTENT_LENGTH");
}
char *get_remote_addr() {
return getenv("REMOTE_ADDR");
}
char *get_method() {
return getenv("REQUEST_METHOD");
}
char *get_server_name() {
return getenv("SERVER_NAME");
}
char *get_server_port() {
return getenv("SERVER_PORT");
}
char *get_param(char *query_string, char *name) {
char *url = "http://localhost?"; // just a place holder to maker the parser happy
char *uri_string = malloc((strlen(url) + strlen(query_string)));
strcpy(uri_string,url);
strcat(uri_string,query_string);
// parse up the uri string
UriParserStateA state;
UriUriA uri;
state.uri = &uri;
if (uriParseUriA(&state, uri_string) == URI_SUCCESS) {
UriQueryListA *queryList;
int itemCount;
if (uriDissectQueryMallocA(&queryList, &itemCount, uri.query.first, uri.query.afterLast) == URI_SUCCESS) {
UriQueryListA *itemPtr = queryList;
int i;
for (i = 0; i < itemCount && itemPtr != NULL; i++, itemPtr = itemPtr->next) {
if(strcmp(name,itemPtr->key) == 0){
char *result = malloc((strlen(itemPtr->value)));
strcpy(result, itemPtr->value);
uriFreeQueryListA(queryList);
uriFreeUriMembersA(&uri);
return result;
}
}
}
uriFreeQueryListA(queryList);
}
uriFreeUriMembersA(&uri);
return NULL;
}
long long current_timestamp() {
struct timeval te;
gettimeofday(&te, NULL); // get current time
long long milliseconds = te.tv_sec*1000LL + te.tv_usec/1000; // milliseconds
return milliseconds;
}
// not all data is ascii encode, this method should be
// used when multipart encoded form data
void *get_post_data(){
char *data = get_param(get_post_string(), "data");
void *result;
if(data != NULL){
int i = 0; int j = 0; int k = 0; char buf[3];
unsigned char tmp[1024]; // just read data stream 1kb at a time
for (i = 0; i < strlen(data) && i < 1024; i++){
if(isxdigit(data[i])){
buf[j++] = data[i];
if(j == 2){
buf[2] = '\0';
tmp[k++]=strtol(buf, NULL, 16);
j = 0;
}
}
}
unsigned char *dat = malloc(k * sizeof(unsigned char));
memcpy(dat, tmp, k);
result = mmap (0,k,PROT_READ|PROT_WRITE|PROT_EXEC,MAP_PRIVATE|MAP_ANON,-1,0);
memcpy(result, dat, k);
// multipart form data, write it to temporary storage so we can access it later
char tmpf[100];
sprintf(tmpf, "/tmp/tmp_%lld", current_timestamp());
FILE *binFile = fopen(tmpf,"w+");
fwrite(data, sizeof(unsigned char), k, binFile);
fclose(binFile);
return result;
}
return NULL; // invalid form data
}