-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathhttp_connection.c
196 lines (159 loc) · 6.44 KB
/
http_connection.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#include "http_connection.h"
char *_string_replace(char *string, const char *substr, const char *replacement){
char *tok = NULL;
char *newstr = NULL;
char *oldstr = NULL;
char *head = NULL;
/* if either substr or replacement is NULL, duplicate string a let caller handle it */
if ( substr == NULL || replacement == NULL ) return strdup (string);
newstr = strdup (string);
head = newstr;
while ( (tok = strstr ( head, substr ))){
oldstr = newstr;
newstr = malloc ( strlen ( oldstr ) - strlen ( substr ) + strlen ( replacement ) + 1 );
/*failed to alloc mem, free old string and return NULL */
if ( newstr == NULL ){
free (oldstr);
return (char *)string;
}
memcpy ( newstr, oldstr, tok - oldstr );
memcpy ( newstr + (tok - oldstr), replacement, strlen ( replacement ) );
memcpy ( newstr + (tok - oldstr) + strlen( replacement ), tok + strlen ( substr ), strlen ( oldstr ) - strlen ( substr ) - ( tok - oldstr ) );
memset ( newstr + strlen ( oldstr ) - strlen ( substr ) + strlen ( replacement ) , 0, 1 );
/* move back head right after the last replacement */
head = newstr + (tok - oldstr) + strlen( replacement );
free (oldstr);
}
free(string);
return newstr;
}
char *_html_escape_string(char *string) {
const char ignore_escape_characters[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '-', '.', '_', '~'};
char *escaped_string = calloc(strlen(string) * 3, sizeof(char));
int i, j;
for(i = 0; i < strlen(string); i++) {
unsigned char current_char = string[i];
int replace_current_char = 1;
for(j = 0; j < sizeof(ignore_escape_characters)/sizeof(char); j++) {
if(ignore_escape_characters[j] == current_char) {
char *new_string = malloc(sizeof(char) * 3);
sprintf(new_string, "%c", current_char);
strcat(escaped_string, new_string);
free(new_string);
replace_current_char = 0;
break;
}
}
if(replace_current_char == 1) {
char *new_string = malloc(sizeof(char) * 15);
sprintf(new_string, "%%%02X", current_char);
strcat(escaped_string, new_string);
free(new_string);
}
}
return escaped_string;
}
size_t _write_data( void *buffer, size_t size, size_t nmemb, void *userp )
{
HTTPConnection *http_connection = (HTTPConnection *)userp;
int segsize = size * nmemb;
char *response_buffer = http_connection->response_buffer;
memcpy(((void *)response_buffer) + http_connection->response_buffer_length, buffer, (size_t)segsize);
// Update the write index
http_connection->response_buffer_length += segsize;
// Null terminate the buffer
response_buffer[http_connection->response_buffer_length] = 0;
// Return the number of bytes received, indicating to curl that all is okay
return segsize;
}
void HTTPConnection_initialize(HTTPConnection *http_connection) {
http_connection->first_parameter = NULL;
http_connection->url = NULL;
http_connection->response_buffer_length = 0;
http_connection->response_buffer = malloc(sizeof(char) * MAX_BUFFER);
http_connection->connection_method = HTTPConnectionMethodGET;
}
void HTTPConnection_free(HTTPConnection *http_connection) {
free(http_connection->response_buffer);
free(http_connection->url);
if(http_connection->first_parameter) {
HTTPParameter_free(http_connection->first_parameter, 1);
}
http_connection = NULL;
}
void HTTPParameter_initialize(HTTPParameter *http_parameter) {
http_parameter->type = HTTPParameterTypeParameter;
http_parameter->key = NULL;
http_parameter->value = NULL;
http_parameter->next_parameter = NULL;
http_parameter->previous_parameter = NULL;
}
void _HTTPParameter_free(HTTPParameter *http_parameter) {
free(http_parameter->key);
free(http_parameter->value);
free(http_parameter);
http_parameter = NULL;
}
void HTTPParameter_free(HTTPParameter *http_parameter, int free_related_params) {
if(free_related_params != 0) {
HTTPParameter *current_parameter = http_parameter;
while(current_parameter != NULL) {
HTTPParameter *new_current_parameter = (HTTPParameter *)current_parameter->next_parameter;
_HTTPParameter_free(current_parameter);
current_parameter = new_current_parameter;
}
} else {
_HTTPParameter_free(http_parameter);
}
}
int HTTPConnection_perform_request(HTTPConnection *http_connection) {
CURL *curl;
CURLcode ret;
http_connection->response_buffer_length = 0;
curl = curl_easy_init();
if (!curl) {
printf("Unable to initialize cURL.\n");
return -1;
}
curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)http_connection); // sets userp
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, _write_data); // sets write function
if(http_connection->connection_method == HTTPConnectionMethodPOST) {
curl_easy_setopt(curl, CURLOPT_POST, 1);
}
struct curl_slist *headers = NULL; /* init to NULL is important */
char *parameters_string = malloc(sizeof(char) * 1000);
if(http_connection->first_parameter != NULL) {
HTTPParameter *current_parameter = http_connection->first_parameter;
while(current_parameter != NULL) {
char *current_parameter_string = malloc(sizeof(char) * 1000);
if(current_parameter->type == HTTPParameterTypeParameter) {
sprintf(current_parameter_string, "%s=%s&", current_parameter->key, current_parameter->value);
strcat(parameters_string, current_parameter_string);
} else if(current_parameter->type == HTTPParameterTypeHeader) {
sprintf(current_parameter_string, "%s: %s", current_parameter->key, current_parameter->value);
headers = curl_slist_append(headers, current_parameter_string);
}
free(current_parameter_string);
current_parameter = (HTTPParameter *)current_parameter->next_parameter;
}
parameters_string[strlen(parameters_string) - 1] = '\0';
if(http_connection->connection_method == HTTPConnectionMethodPOST) {
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, parameters_string);
} else {
strcat(http_connection->url, "?");
strcat(http_connection->url, parameters_string);
}
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
}
curl_easy_setopt(curl, CURLOPT_URL, http_connection->url); // sets url
// curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
ret = curl_easy_perform(curl);
curl_slist_free_all(headers);
curl_easy_cleanup(curl);
free(parameters_string);
if (ret != 0) {
printf("cURL error (%i): %s\n", ret, curl_easy_strerror(ret));
return ret;
}
return 0;
}