#include #include #include #include #include #include #include #include #ifdef __WIN32__ #include #endif #include #include #define HTTP_PORT 8000 #define HTTP_URL "http://127.0.0.1:8000/" #if MHD_VERSION < 0x00040000 int content_read_callback (void* cls, size_t pos, char* buf, int max) #else int content_read_callback (void* cls, uint64_t pos, char* buf, int max) #endif { const char* body = (char*)cls; size_t bodylen = strlen(body); if (pos >= bodylen) return -1; size_t len = ((size_t)max <= bodylen - pos ? max : bodylen - pos); memcpy(buf, body, len); return len; } void content_free_callback (void *cls) { } int on_request_data (void *cls, struct MHD_Connection *connection, const char *url, const char *method, const char *version, const char *upload_data, unsigned int *upload_data_size, void **con_cls) { //on first call create data structure struct MHD_Response* response = NULL; unsigned int status_code = MHD_HTTP_OK; //create response const char* body = "Hello world!\n"; response = MHD_create_response_from_callback(MHD_SIZE_UNKNOWN, 2048, content_read_callback, (void*)body, content_free_callback); if (!response) { status_code = MHD_HTTP_NOT_FOUND; response = MHD_create_response_from_data(9, (void*)"Not found", MHD_NO, MHD_NO); MHD_add_response_header(response, MHD_HTTP_HEADER_CONTENT_TYPE, "text/html"); } //add header MHD_add_response_header(response, MHD_HTTP_HEADER_CONTENT_TYPE, "text/plain"); //send response int ret = MHD_queue_response(connection, status_code, response); MHD_destroy_response(response); return ret; } void on_request_completed (void* cls, struct MHD_Connection* connection, void** con_cls, enum MHD_RequestTerminationCode toe) { } int main (int argc, char *const *argv) { //start HTTP server struct MHD_Daemon* webserver; if ((webserver = MHD_start_daemon(MHD_USE_THREAD_PER_CONNECTION | MHD_USE_DEBUG | MHD_USE_PEDANTIC_CHECKS, HTTP_PORT, NULL, NULL, &on_request_data, NULL, MHD_OPTION_NOTIFY_COMPLETED, on_request_completed, NULL, MHD_OPTION_END)) == NULL) { fprintf(stderr, "Error starting webserver on port %i\n", (int)HTTP_PORT); return 1; } //HTTP client CURL *curl; CURLcode res; if ((curl = curl_easy_init()) != NULL) { curl_easy_setopt(curl, CURLOPT_URL, HTTP_URL); //curl_easy_setopt(curl, CURLOPT_HTTPHEADER, 1L); curl_easy_setopt(curl, CURLOPT_WRITEHEADER, stdout); curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 1L); res = curl_easy_perform(curl); curl_easy_cleanup(curl); } //stop HTTP server MHD_stop_daemon(webserver); return 0; }