28 #include "webserver.h"
33 #include <curl/curl.h>
39 static hpd_ws_t *
ws = NULL;
40 static struct ev_loop *
loop;
43 static size_t data_from_curl(
char *buffer,
size_t buffer_size,
size_t nmemb,
char **userdata)
45 *userdata = realloc(*userdata, strlen(*userdata) + buffer_size*nmemb + 1);
46 char *
data = &(*userdata)[strlen(*userdata)];
47 memcpy(data, buffer, buffer_size*nmemb);
48 data[buffer_size*nmemb] =
'\0';
50 return buffer_size*nmemb;
56 size_t *sent = userdata;
62 strncpy(buf, data, size*nmemb);
65 if (strlen(data) < size*nmemb) {
78 CURL *handle = curl_easy_init();
80 char* userdata = malloc(
sizeof(
char));
86 curl_easy_setopt(handle, CURLOPT_URL, url);
87 curl_easy_setopt(handle, CURLOPT_UPLOAD, 1L);
89 curl_easy_setopt(handle, CURLOPT_WRITEDATA, &userdata);
90 curl_easy_setopt(handle, CURLOPT_READFUNCTION,
data_to_curl);
91 curl_easy_setopt(handle, CURLOPT_READDATA, &sent);
95 struct curl_slist *chunk = NULL;
96 chunk = curl_slist_append(chunk,
"Transfer-Encoding:");
97 curl_easy_setopt(handle, CURLOPT_HTTPHEADER, chunk);
99 if((c = curl_easy_perform(handle)) != CURLE_OK)
101 fprintf(stderr,
"curl_easy_perform failed: %s\n", curl_easy_strerror(c));
105 curl_slist_free_all(chunk);
106 curl_easy_cleanup(handle);
111 perror(
"Could not initialize curl: ");
121 if(strstr(received,
big_data) != NULL) {
124 printf(
"The following bad string was received: %s\n", received);
138 curl_global_init(CURL_GLOBAL_ALL);
141 printf(
"Running tcpd tests\n");
145 if (testresult == 1) {
146 printf(
"Test succeeded\n");
148 printf(
"Test failed\n");
153 curl_global_cleanup();
160 hpd_ws_conn_t *conn,
void *ws_ctx,
void **
data)
162 char *body = malloc(
sizeof(
char));
171 hpd_ws_conn_t *conn,
void *ctx,
void **
data,
172 const char *buf,
size_t len)
174 char *body = *
data, *
new;
177 new_len = strlen(body)+len+1;
178 new = realloc(body,
sizeof(
char)*new_len);
181 strncat(body, buf, len);
182 body[new_len-1] =
'\0';
185 fprintf(stderr,
"Reallocation failed\n");
188 if (strstr(body,
"EOF") != NULL) {
189 ws_conn_sendf(conn,
"HTTP/1.1 200 OK\r\n\r\n%s", body);
198 hpd_ws_conn_t *conn,
void *ws_ctx,
void **
data)
207 fprintf(stderr,
"Sending stop signal\n");
212 static void exit_cb(EV_P_ ev_async *watcher,
int revents)
214 fprintf(stderr,
"Stopping tcpd\n");
221 ev_break(
loop, EVBREAK_ALL);
235 struct ws_settings settings = WS_SETTINGS_DEFAULT;
236 settings.port = HPD_TCPD_P_HTTP_ALT;
243 printf(
"Debugging is set\n");
251 ws = ws_create(&settings,
loop);
262 int main(
int argc,
char *argv[])
265 pthread_t server_thread;
267 unsigned int i, size = 1024*1024;
268 big_data = malloc(
sizeof(
char)*size);
269 for (i = 0; i < size; i++) {
270 char c = rand() % (
'z' -
'a' + 1) +
'a';
288 pthread_join(server_thread, NULL);
static void exit_handler(int sig)
Handle correct exiting.
static struct ev_async exit_watcher
static void * webserver_thread(void *arg)
Webserver thread.
static int on_connect(hpd_ws_t *instance, hpd_ws_conn_t *conn, void *ws_ctx, void **data)
On connect callback for tcpd.
static int basic_get_test(char *url)
static int on_disconnect(hpd_ws_t *instance, hpd_ws_conn_t *conn, void *ws_ctx, void **data)
On disconnect callback for tcpd.
static int on_receive(hpd_ws_t *instance, hpd_ws_conn_t *conn, void *ctx, void **data, const char *buf, size_t len)
On receive callback for tcpd.
static char * simple_get_request(char *url)
int main(int argc, char *argv[])
Main function.
static struct ev_loop * loop
static size_t data_from_curl(char *buffer, size_t buffer_size, size_t nmemb, char **userdata)
static void exit_cb(EV_P_ ev_async *watcher, int revents)
Exit callback for async watcher (Webserver)
static size_t data_to_curl(void *ptr, size_t size, size_t nmemb, void *userdata)
static int test_thread()
Test thread.