28 #include "http-webserver.h"
29 #include "unit_test.h"
34 #include <curl/curl.h>
53 static struct httpws *
ws = NULL;
54 static struct ev_loop *
loop;
57 static size_t data_from_curl(
char *buffer,
size_t buffer_size,
size_t nmemb,
char **userdata)
59 *userdata = realloc(*userdata, strlen(*userdata) + buffer_size*nmemb + 1);
60 char *
data = &(*userdata)[strlen(*userdata)];
61 memcpy(data, buffer, buffer_size*nmemb);
62 data[buffer_size*nmemb] =
'\0';
64 return buffer_size*nmemb;
70 size_t *sent = userdata;
75 printf(
"Data to CURL\n");
78 strncpy(buf, data, size*nmemb);
81 if (strlen(data) < size*nmemb) {
95 CURL *handle = curl_easy_init();
97 char* userdata = malloc(
sizeof(
char));
103 curl_easy_setopt(handle, CURLOPT_URL, url);
104 curl_easy_setopt(handle, CURLOPT_UPLOAD, 1L);
106 curl_easy_setopt(handle, CURLOPT_WRITEDATA, &userdata);
107 curl_easy_setopt(handle, CURLOPT_READFUNCTION,
data_to_curl);
108 curl_easy_setopt(handle, CURLOPT_READDATA, &sent);
109 curl_easy_setopt(handle, CURLOPT_INFILESIZE, strlen(
req_data));
113 struct curl_slist *chunk = NULL;
114 chunk = curl_slist_append(chunk,
"Transfer-Encoding:");
115 chunk = curl_slist_append(chunk,
116 "Cookie: cookie1=val1; cookie2=val2");
117 curl_easy_setopt(handle, CURLOPT_HTTPHEADER, chunk);
119 if((c = curl_easy_perform(handle)) != CURLE_OK)
121 fprintf(stderr,
"curl_easy_perform failed: %s\n", curl_easy_strerror(c));
125 curl_slist_free_all(chunk);
126 curl_easy_cleanup(handle);
131 perror(
"Could not initialize curl: ");
139 int len = strlen(host) + strlen(path) + 1;
140 char *
url = malloc(len*
sizeof(
char));
151 len = strlen(http_method_str(method)) + strlen(path) + 2;
152 char *first_line = malloc(len*
sizeof(
char));
153 strcpy(first_line, http_method_str(method));
154 strcat(first_line,
" ");
155 strcat(first_line, path);
158 len = snprintf(
"", 0,
"Content-Length: %i\r\n", strlen(
req_data));
159 char *content_length = malloc(len*
sizeof(
char));
160 snprintf(content_length, len,
"Content-Length: %i\r\n",
164 if (strstr(res, first_line) == NULL) {
166 printf(
"The following bad string was received: %s\n", res);
170 if (strstr(res, content_length) == NULL) {
172 printf(
"The following bad string was received: %s\n", res);
178 printf(
"The following bad string was received: %s\n", res);
183 free(content_length);
195 curl_global_init(CURL_GLOBAL_ALL);
198 printf(
"Running webserver tests\n");
199 testresult =
basic_get_test(HTTP_PUT,
"http://localhost:8080",
"/");
203 printf(
"Test failed\n");
206 printf(
"Test succeeded\n");
210 curl_global_cleanup();
215 static char *
ncat(
char *s1,
const char *s2,
int s2_len)
217 int len = s2_len + 1;
218 if (s1 != NULL) len += strlen(s1);
219 char *str = realloc(s1, len);
220 if (s1 == NULL) str[0] =
'\0';
221 strncat(str, s2, s2_len);
226 struct httpws *ins,
struct http_request *req,
229 struct data *
data = malloc(
sizeof(
struct data));
247 struct httpws *ins,
struct http_request *req,
249 const char *buf,
size_t len)
263 struct httpws *ins,
struct http_request *req,
265 const char *buf,
size_t len)
279 struct httpws *ins,
struct http_request *req,
290 const char *
url = http_request_get_url(req);
298 struct httpws *ins,
struct http_request *req,
300 const char *buf,
size_t len)
315 str = &str[strlen(str)+1];
317 strncpy(str, buf, len);
325 struct httpws *ins,
struct http_request *req,
327 const char *buf,
size_t len)
342 str = &str[strlen(str)+1];
344 strncpy(str, buf, len);
354 struct httpws *ins,
struct http_request *req,
367 const char *got = http_request_get_header(req, f);
379 struct httpws *ins,
struct http_request *req,
381 const char *buf,
size_t len)
400 l += snprintf(body, len,
"%d %s %s\r\n", data->
_errors, data->
method, data->
url);
402 l += snprintf(&body[l], len,
"%s: %s\r\n", field, value);
403 field = &field[strlen(field)+1];
404 value = &value[strlen(value)+1];
406 if (data->
body != NULL)
407 l += snprintf(&body[l], len,
"\r\n%s", data->
body);
413 struct httpws *ins,
struct http_request *req,
424 enum http_method
method = http_request_get_method(req);
425 if (strcmp(data->
method, http_method_str(method)) != 0) {
426 fprintf(stderr,
"Got '%s', Expected '%s'\n", data->
method,
427 http_method_str(method));
433 cookie = http_request_get_cookie(req,
"cookie1");
435 cookie = http_request_get_cookie(req,
"cookie2");
442 body = malloc(body_len*
sizeof(
char));
446 struct http_response *res = http_response_create(req, WS_HTTP_200);
447 http_response_sendf(res, body);
448 http_response_destroy(res);
464 fprintf(stderr,
"Sending stop signal\n");
469 static void exit_cb(EV_P_ ev_async *watcher,
int revents)
471 fprintf(stderr,
"Stopping webserver\n");
478 ev_break(
loop, EVBREAK_ALL);
492 struct httpws_settings settings = HTTPWS_SETTINGS_DEFAULT;
493 settings.port = HPD_TCPD_P_HTTP_ALT;
506 printf(
"Debugging is set\n");
514 ws = httpws_create(&settings,
loop);
525 int main(
int argc,
char *argv[])
528 pthread_t server_thread;
540 pthread_join(server_thread, NULL);
static void * webserver_thread(void *arg)
Webserver thread.
static void exit_cb(EV_P_ ev_async *watcher, int revents)
Exit callback for async watcher (Webserver)
static int test_thread()
Test thread.
static int on_req_body(struct httpws *ins, struct http_request *req, void *ws_ctx, void **req_data, const char *buf, size_t len)
static int on_req_hdr_cmpl(struct httpws *ins, struct http_request *req, void *ws_ctx, void **req_data)
static int construct_body(char *body, int len, struct data *data)
static char * ncat(char *s1, const char *s2, int s2_len)
ASSERT_STR_EQUAL(data.url, url)
static int on_req_hdr_field(struct httpws *ins, struct http_request *req, void *ws_ctx, void **req_data, const char *buf, size_t len)
static int on_req_method(struct httpws *ins, struct http_request *req, void *ws_ctx, void **req_data, const char *buf, size_t len)
static int basic_get_test(enum http_method method, char *host, char *path)
static int on_req_cmpl(struct httpws *ins, struct http_request *req, void *ws_ctx, void **req_data)
static int on_req_hdr_value(struct httpws *ins, struct http_request *req, void *ws_ctx, void **req_data, const char *buf, size_t len)
static struct httpws * ws
static int on_req_url(struct httpws *ins, struct http_request *req, void *ws_ctx, void **req_data, const char *buf, size_t len)
static void exit_handler(int sig)
Handle correct exiting.
static struct ev_async exit_watcher
int main(int argc, char *argv[])
Main function.
static struct ev_loop * loop
static int on_req_begin(struct httpws *ins, struct http_request *req, void *ws_ctx, void **req_data)
ASSERT_EQUAL(data.count, 2)
static size_t data_to_curl(void *ptr, size_t size, size_t nmemb, void *userdata)
static int on_req_url_cmpl(struct httpws *ins, struct http_request *req, void *ws_ctx, void **req_data)
static size_t data_from_curl(char *buffer, size_t buffer_size, size_t nmemb, char **userdata)
static char * simple_get_request(char *url)