static int ahc_echo (void *cls, struct MHD_Connection *connection, const char *url, const char *method, const char *version, const char *upload_data, size_t *upload_data_size, void **ptr) { struct MHD_Response *response; char *username; const char *password = "******"; const char *realm = "*****@*****.**"; int ret; (void)cls; /* Unused. Silent compiler warning. */ (void)url; /* Unused. Silent compiler warning. */ (void)method; /* Unused. Silent compiler warning. */ (void)version; /* Unused. Silent compiler warning. */ (void)upload_data; /* Unused. Silent compiler warning. */ (void)upload_data_size; /* Unused. Silent compiler warning. */ (void)ptr; /* Unused. Silent compiler warning. */ username = MHD_digest_auth_get_username(connection); if (NULL == username) { response = MHD_create_response_from_buffer(strlen (DENIED), DENIED, MHD_RESPMEM_PERSISTENT); ret = MHD_queue_auth_fail_response(connection, realm, MY_OPAQUE_STR, response, MHD_NO); MHD_destroy_response(response); return ret; } ret = MHD_digest_auth_check(connection, realm, username, password, 300); MHD_free (username); if ( (ret == MHD_INVALID_NONCE) || (ret == MHD_NO) ) { response = MHD_create_response_from_buffer(strlen (DENIED), DENIED, MHD_RESPMEM_PERSISTENT); if (NULL == response) return MHD_NO; ret = MHD_queue_auth_fail_response(connection, realm, MY_OPAQUE_STR, response, (ret == MHD_INVALID_NONCE) ? MHD_YES : MHD_NO); MHD_destroy_response(response); return ret; } response = MHD_create_response_from_buffer(strlen(PAGE), PAGE, MHD_RESPMEM_PERSISTENT); ret = MHD_queue_response(connection, MHD_HTTP_OK, response); MHD_destroy_response(response); return ret; }
int answer_to_connection( void* cls, struct MHD_Connection* connection, const char* url, const char* method, const char* version, const char* upload_data, size_t* upload_data_size, void** con_cls) { char* user; char* pass; int authorized; struct MHD_Response* response; int ret; if (strncmp(method, "GET", 4) != 0) { return MHD_NO; } if (*con_cls == NULL) { *con_cls = connection; return MHD_YES; } pass = NULL; user = MHD_basic_auth_get_username_password(connection, &pass); authorized = (user != NULL) && (strncmp(user, USER, strlen(USER)) == 0) && (strncmp(pass, PASSWORD, strlen(PASSWORD)) == 0); if (user != NULL) { free(user); } if (pass != NULL) { free(pass); } if (authorized) { const char* page = "<html><body>Authorized!</body></html>"; response = MHD_create_response_from_buffer( strlen(page), (void*)page, MHD_RESPMEM_PERSISTENT); ret = MHD_queue_response(connection, MHD_HTTP_OK, response); } else { const char* page = "<html><body>Unauthorized!</body></html>"; response = MHD_create_response_from_buffer( strlen(page), (void*)page, MHD_RESPMEM_PERSISTENT); ret = MHD_queue_basic_auth_fail_response( connection, "Tutorial Example 5 Realm", response); } MHD_destroy_response(response); return ret; }
/** * Entry point to demo. Note: this HTTP server will make all * files in the current directory and its subdirectories available * to anyone. Press ENTER to stop the server once it has started. * * @param argc number of arguments in argv * @param argv first and only argument should be the port number * @return 0 on success */ int main (int argc, char *const *argv) { struct MHD_Daemon *d; unsigned int port; if ( (argc != 2) || (1 != sscanf (argv[1], "%u", &port)) || (UINT16_MAX < port) ) { fprintf (stderr, "%s PORT\n", argv[0]); return 1; } #ifndef MINGW ignore_sigpipe (); #endif magic = magic_open (MAGIC_MIME_TYPE); (void) magic_load (magic, NULL); (void) pthread_mutex_init (&mutex, NULL); file_not_found_response = MHD_create_response_from_buffer (strlen (FILE_NOT_FOUND_PAGE), (void *) FILE_NOT_FOUND_PAGE, MHD_RESPMEM_PERSISTENT); mark_as_html (file_not_found_response); request_refused_response = MHD_create_response_from_buffer (strlen (REQUEST_REFUSED_PAGE), (void *) REQUEST_REFUSED_PAGE, MHD_RESPMEM_PERSISTENT); mark_as_html (request_refused_response); internal_error_response = MHD_create_response_from_buffer (strlen (INTERNAL_ERROR_PAGE), (void *) INTERNAL_ERROR_PAGE, MHD_RESPMEM_PERSISTENT); mark_as_html (internal_error_response); update_directory (); d = MHD_start_daemon (MHD_USE_AUTO | MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG, port, NULL, NULL, &generate_page, NULL, MHD_OPTION_CONNECTION_MEMORY_LIMIT, (size_t) (256 * 1024), #if PRODUCTION MHD_OPTION_PER_IP_CONNECTION_LIMIT, (unsigned int) (64), #endif MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) (120 /* seconds */), MHD_OPTION_THREAD_POOL_SIZE, (unsigned int) NUMBER_OF_THREADS, MHD_OPTION_NOTIFY_COMPLETED, &response_completed_callback, NULL, MHD_OPTION_END); if (NULL == d) return 1; fprintf (stderr, "HTTP server running. Press ENTER to stop the server\n"); (void) getc (stdin); MHD_stop_daemon (d); MHD_destroy_response (file_not_found_response); MHD_destroy_response (request_refused_response); MHD_destroy_response (internal_error_response); update_cached_response (NULL); (void) pthread_mutex_destroy (&mutex); magic_close (magic); return 0; }
Private() : mhd(0) { acceptPolicy = [](const QHostAddress &addr) { return addr.isLoopback(); }; const char *msg404 = "<html><body>404 - Page not found</body></html>"; response404 = MHD_create_response_from_buffer(strlen(msg404), const_cast<char*>(msg404), MHD_RESPMEM_PERSISTENT); const char *msg401 = "<html><body>401 - Unauthorized</body></html>"; response401 = MHD_create_response_from_buffer(strlen(msg401), const_cast<char*>(msg401), MHD_RESPMEM_PERSISTENT); }
static int ahc_echo (void *cls, struct MHD_Connection *connection, const char *url, const char *method, const char *version, const char *upload_data, size_t *upload_data_size, void **unused) { struct MHD_Response *response; char *username; const char *password = "******"; const char *realm = "*****@*****.**"; int ret; username = MHD_digest_auth_get_username(connection); if ( (username == NULL) || (0 != strcmp (username, "testuser")) ) { response = MHD_create_response_from_buffer(strlen (DENIED), DENIED, MHD_RESPMEM_PERSISTENT); ret = MHD_queue_auth_fail_response(connection, realm, OPAQUE, response, MHD_NO); MHD_destroy_response(response); return ret; } ret = MHD_digest_auth_check(connection, realm, username, password, 300); free(username); if ( (ret == MHD_INVALID_NONCE) || (ret == MHD_NO) ) { response = MHD_create_response_from_buffer(strlen (DENIED), DENIED, MHD_RESPMEM_PERSISTENT); if (NULL == response) return MHD_NO; ret = MHD_queue_auth_fail_response(connection, realm, OPAQUE, response, (ret == MHD_INVALID_NONCE) ? MHD_YES : MHD_NO); MHD_destroy_response(response); return ret; } response = MHD_create_response_from_buffer(strlen(PAGE), PAGE, MHD_RESPMEM_PERSISTENT); ret = MHD_queue_response(connection, MHD_HTTP_OK, response); MHD_destroy_response(response); return ret; }
static int ahc_echo (void *cls, struct MHD_Connection *connection, const char *url, const char *method, const char *version, const char *upload_data, size_t *upload_data_size, void **ptr) { static int aptr; const char *me = cls; struct MHD_Response *response; int ret; char *user; char *pass; int fail; if (0 != strcmp (method, "GET")) return MHD_NO; /* unexpected method */ if (&aptr != *ptr) { /* do never respond on first call */ *ptr = &aptr; return MHD_YES; } *ptr = NULL; /* reset when done */ /* require: "Aladdin" with password "open sesame" */ pass = NULL; user = MHD_basic_auth_get_username_password (connection, &pass); fail = ( (user == NULL) || (0 != strcmp (user, "Aladdin")) || (0 != strcmp (pass, "open sesame") ) ); if (fail) { response = MHD_create_response_from_buffer (strlen (DENIED), (void *) DENIED, MHD_RESPMEM_PERSISTENT); ret = MHD_queue_basic_auth_fail_response (connection,"TestRealm",response); } else { response = MHD_create_response_from_buffer (strlen (me), (void *) me, MHD_RESPMEM_PERSISTENT); ret = MHD_queue_response (connection, MHD_HTTP_OK, response); } if (NULL != user) free (user); if (NULL != pass) free (pass); MHD_destroy_response (response); return ret; }
static int ahc_echo (void *cls, struct MHD_Connection *connection, const char *url, const char *method, const char *version, const char *upload_data, size_t *upload_data_size, void **ptr) { static int aptr; const char *me = cls; struct MHD_Response *response; int ret; if (0 != strcmp (method, "GET")) return MHD_NO; /* unexpected method */ if (&aptr != *ptr) { /* do never respond on first call */ *ptr = &aptr; return MHD_YES; } *ptr = NULL; /* reset when done */ response = MHD_create_response_from_buffer (strlen (me), (void *) me, MHD_RESPMEM_PERSISTENT); ret = MHD_queue_response (connection, MHD_HTTP_OK, response); MHD_destroy_response (response); return ret; }
int main (int argc, char *const *argv) { unsigned int errorCount = 0; int port = 1081; oneone = (NULL != strrchr (argv[0], (int) '/')) ? (NULL != strstr (strrchr (argv[0], (int) '/'), "11")) : 0; if (0 != curl_global_init (CURL_GLOBAL_WIN32)) return 2; response = MHD_create_response_from_buffer (strlen ("/hello_world"), "/hello_world", MHD_RESPMEM_MUST_COPY); errorCount += testExternalGet (port++); errorCount += testInternalGet (port++, 0); errorCount += testMultithreadedGet (port++, 0); errorCount += testMultithreadedPoolGet (port++, 0); if (MHD_YES == MHD_is_feature_supported(MHD_FEATURE_POLL)) { errorCount += testInternalGet(port++, MHD_USE_POLL); errorCount += testMultithreadedGet(port++, MHD_USE_POLL); errorCount += testMultithreadedPoolGet(port++, MHD_USE_POLL); } if (MHD_YES == MHD_is_feature_supported(MHD_FEATURE_EPOLL)) { errorCount += testInternalGet(port++, MHD_USE_EPOLL_LINUX_ONLY); errorCount += testMultithreadedPoolGet(port++, MHD_USE_EPOLL_LINUX_ONLY); } MHD_destroy_response (response); if (errorCount != 0) fprintf (stderr, "Error (code: %u)\n", errorCount); curl_global_cleanup (); return errorCount != 0; /* 0 == pass */ }
/** * Add an XML response to the queue of the server * * @param connection The client connection which will receive the response * * @param xmlbuff The XML to send * * @return MHD return value, MHD_NO if the response failed to be created, * return code of MHD_queue_response otherwise */ static int send_xml ( struct MHD_Connection *connection, char *xmlbuff ) { int ret; struct MHD_Response *response; if( !xmlbuff ) { printf("The XML that is attempted to send is NULL\n"); return MHD_NO; } response = MHD_create_response_from_buffer (strlen(xmlbuff), xmlbuff, MHD_RESPMEM_MUST_FREE); if( !response ) { if( xmlbuff ) free( xmlbuff ); return MHD_NO; } MHD_add_response_header ( response, "Content-Type", "text/xml" ); ret = MHD_queue_response ( connection, MHD_HTTP_OK, response ); MHD_destroy_response( response ); return ret; }
inline int sendMethodNotAllowedResponse(struct MHD_Connection* connection, bool allowGet) { struct MHD_Response* response; int ret; #ifdef MICROHTTPD_DEPRECATED response = MHD_create_response_from_data( strlen(XML_MWS_METHOD_NOT_ALLOWED), (void*)XML_MWS_METHOD_NOT_ALLOWED, false, false); #else // MICROHTTPD_DEPRECATED response = MHD_create_response_from_buffer( strlen(XML_MWS_METHOD_NOT_ALLOWED), (void*)XML_MWS_METHOD_NOT_ALLOWED, MHD_RESPMEM_PERSISTENT); #endif // MICROHTTPD_DEPRECATED MHD_add_response_header(response, "Content-Type", "text/xml"); MHD_add_response_header(response, "Allow", allowGet ? "GET, POST, OPTIONS" : "POST, OPTIONS"); MHD_add_response_header(response, "Access-Control-Allow-Origin", "*"); MHD_add_response_header(response, "Allow", allowGet ? "GET, POST, OPTIONS" : "POST, OPTIONS"); MHD_add_response_header(response, "Access-Control-Allow-Headers", "CONTENT-TYPE"); MHD_add_response_header(response, "Access-Control-Max-Age", "1728000"); ret = MHD_queue_response(connection, MHD_HTTP_METHOD_NOT_ALLOWED, response); MHD_destroy_response(response); return ret; }
inline int sendOptionsResponse(struct MHD_Connection* connection) { struct MHD_Response* response; int ret; #ifdef MICROHTTPD_DEPRECATED response = MHD_create_response_from_data(strlen(EMPTY_RESPONSE), (void*) EMPTY_RESPONSE, false, false); #else // MICROHTTPD_DEPRECATED response = MHD_create_response_from_buffer(strlen(EMPTY_RESPONSE), (void*) EMPTY_RESPONSE, MHD_RESPMEM_PERSISTENT); #endif // MICROHTTPD_DEPRECATED MHD_add_response_header(response, "Content-Type", "text/plain"); MHD_add_response_header(response, "Access-Control-Allow-Origin", "*"); MHD_add_response_header(response, "Access-Control-Allow-Methods", "POST, OPTIONS"); MHD_add_response_header(response, "Access-Control-Allow-Headers", "CONTENT-TYPE"); MHD_add_response_header(response, "Access-Control-Max-Age", "1728000"); ret = MHD_queue_response(connection, MHD_HTTP_OK, response); MHD_destroy_response(response); return ret; }
inline int sendXmlGenericResponse(struct MHD_Connection* connection, const char* xmlGenericResponse, int statusCode) { struct MHD_Response* response; int ret; #ifdef MICROHTTPD_DEPRECATED response = MHD_create_response_from_data(strlen(xmlGenericResponse), (void*) xmlGenericResponse, /* must_free = */ 0, /* must_copy = */ 0); #else // MICROHTTPD_DEPRECATED response = MHD_create_response_from_buffer(strlen(xmlGenericResponse), (void*) xmlGenericResponse, MHD_RESPMEM_PERSISTENT); #endif // MICROHTTPD_DEPRECATED MHD_add_response_header(response, "Content-Type", "text/xml"); ret = MHD_queue_response(connection, statusCode, response); MHD_destroy_response(response); return ret; }
/** * Handler that returns a simple static HTTP page that * is passed in via 'cls'. * * @param cls a 'const char *' with the HTML webpage to return * @param mime mime type to use * @param session session handle * @param connection connection to use */ static int serve_simple_form (const void *cls, const char *mime, struct Session *session, struct MHD_Connection *connection) { int ret; const char *form = cls; struct MHD_Response *response; /* return static form */ response = MHD_create_response_from_buffer (strlen (form), (void *) form, MHD_RESPMEM_PERSISTENT); if (NULL == response) return MHD_NO; add_session_cookie (session, response); MHD_add_response_header (response, MHD_HTTP_HEADER_CONTENT_ENCODING, mime); ret = MHD_queue_response (connection, MHD_HTTP_OK, response); MHD_destroy_response (response); return ret; }
/* 将response提交到queue中, 并且销毁 */ int JsSubmitResponse(struct JsObject* obj){ if(strcmp(obj->Class,"Response")!=0) JsThrowString("The Object Is't Response"); struct JsResponse* response = (struct JsResponse*)obj->sb[JS_RESPONSE_FLOOR]; if(response == NULL) JsThrowString("The Response Is Burned"); struct MHD_Response *HTMLResponse = NULL; //构建HTML response HTMLResponse = MHD_create_response_from_buffer (response->bUsed, (void *) response->body, MHD_RESPMEM_MUST_COPY); //配置header int i; for(i=0;i<response->hUsed;++i){ MHD_add_response_header (HTMLResponse, response->header[i].key, response->header[i].value); } //配置状态码 int ret = MHD_queue_response (response->connection, response->code, HTMLResponse); MHD_destroy_response (HTMLResponse); //释放内存 free(response->body); for(i=0;i<response->hUsed;++i){ free(response->header[i].key); free(response->header[i].value); } free(response->header); free(response); obj->sb[JS_RESPONSE_FLOOR] = NULL; return ret; }
static int ahc_empty (void *cls, struct MHD_Connection *connection, const char *url, const char *method, const char *version, const char *upload_data, size_t *upload_data_size, void **unused) { static int ptr; struct MHD_Response *response; int ret; if (0 != strcmp ("GET", method)) return MHD_NO; /* unexpected method */ if (&ptr != *unused) { *unused = &ptr; return MHD_YES; } *unused = NULL; response = MHD_create_response_from_buffer (0, NULL, MHD_RESPMEM_PERSISTENT); ret = MHD_queue_response (connection, MHD_HTTP_OK, response); MHD_destroy_response (response); if (ret == MHD_NO) abort (); return ret; }
static int ahc_echo (void *cls, struct MHD_Connection *connection, const char *url, const char *method, const char *version, const char *upload_data, size_t *upload_data_size, void **mptr) { static int marker; struct MHD_Response *response; int ret; if (0 != strcmp ("POST", method)) { printf ("METHOD: %s\n", method); return MHD_NO; /* unexpected method */ } if ((*mptr != NULL) && (0 == *upload_data_size)) { if (*mptr != &marker) abort (); response = MHD_create_response_from_buffer (2, "OK", MHD_RESPMEM_PERSISTENT); ret = MHD_queue_response (connection, MHD_HTTP_OK, response); MHD_destroy_response (response); *mptr = NULL; return ret; } if (strlen (POST_DATA) != *upload_data_size) return MHD_YES; *upload_data_size = 0; *mptr = ▮ return MHD_YES; }
/* *The Cross-Origin Resource Sharing standard works by adding new HTTP headers *that allow servers to describe the set of origins that are permitted to read *that information using a web browser. Additionally, for HTTP request methods *that can cause side-effects on user data (in particular, for HTTP methods *other than GET, or for POST usage with certain MIME types), the specification *mandates that browsers "preflight" the request, soliciting supported methods *from the server with an HTTP OPTIONS request header, and then, upon *"approval" from the server, sending the actual request with the actual HTTP *request method. Servers can also notify clients whether "credentials" *(including Cookies and HTTP Authentication data) should be sent with *requests. */ static int sendAccessControl(struct MHD_Connection *connection, const char *url, const char *method, const char *version) { int ret; struct MHD_Response *response; std::cout << "Sending CORS accept header for the request: " << std::endl; /*answer_to_connection(NULL, connection, url, method, version, NULL, NULL, NULL);*/ response = MHD_create_response_from_buffer(0, NULL, MHD_RESPMEM_PERSISTENT); if (response == 0) { return MHD_NO; } // not too fussed with who is trying to use us :) MHD_add_response_header(response, "Access-Control-Allow-Origin", "*"); // only allow GET (and OPTIONS) requests, no need for PUSH yet...now there is a need for push MHD_add_response_header(response, "Access-Control-Allow-Methods", "GET, OPTIONS, POST"); // see http://stackoverflow.com/questions/107390/whats-the-difference-between-a-post-and-a-put-http-request // we simply 'allow' all requested headers const char* val = MHD_lookup_connection_value(connection, MHD_HEADER_KIND, "Access-Control-Request-Headers"); MHD_add_response_header(response, "Access-Control-Allow-Headers", val); // these seem to be needed? MHD_add_response_header(response, "Access-Control-Expose-Headers", "Content-Range"); ret = MHD_queue_response(connection, MHD_HTTP_OK, response); MHD_destroy_response(response); return ret; }
/** * Send the 'SUBMIT_PAGE'. * * @param info information string to send to the user * @param request request information * @param connection connection to use */ static int fill_s_reply (const char *info, struct Request *request, struct MHD_Connection *connection) { int ret; char *reply; struct MHD_Response *response; GNUNET_asprintf (&reply, SUBMIT_PAGE, info, info); /* return static form */ response = MHD_create_response_from_buffer (strlen (reply), (void *) reply, MHD_RESPMEM_MUST_FREE); MHD_add_response_header (response, MHD_HTTP_HEADER_CONTENT_TYPE, MIME_HTML); ret = MHD_queue_response (connection, MHD_HTTP_OK, response); MHD_destroy_response (response); return ret; }
static int answer_to_connection(void *cls, struct MHD_Connection *connection, const char *url, const char *method, const char *version, const char *upload_data, size_t *upload_data_size, void **con_cls) { const char *page = "<html><body>Hello timeout!</body></html>"; struct MHD_Response *response; int ret; (void)cls; /* Unused. Silent compiler warning. */ (void)url; /* Unused. Silent compiler warning. */ (void)version; /* Unused. Silent compiler warning. */ (void)method; /* Unused. Silent compiler warning. */ (void)upload_data; /* Unused. Silent compiler warning. */ (void)upload_data_size; /* Unused. Silent compiler warning. */ (void)con_cls; /* Unused. Silent compiler warning. */ response = MHD_create_response_from_buffer (strlen(page), (void *) page, MHD_RESPMEM_PERSISTENT); MHD_add_response_header (response, MHD_HTTP_HEADER_CONTENT_TYPE, "text/html"); ret = MHD_queue_response (connection, MHD_HTTP_OK, response); MHD_destroy_response(response); return ret; }
static int mhd_ahc (void *cls, struct MHD_Connection *connection, const char *url, const char *method, const char *version, const char *upload_data, size_t *upload_data_size, void **unused) { static int ptr; struct MHD_Response *response; int ret; if (0 != strcmp ("GET", method)) return MHD_NO; /* unexpected method */ if (&ptr != *unused) { *unused = &ptr; return MHD_YES; } *unused = NULL; GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MHD sends respose for request to URL `%s'\n", url); response = MHD_create_response_from_buffer (strlen (url), (void *) url, MHD_RESPMEM_MUST_COPY); ret = MHD_queue_response (connection, MHD_HTTP_OK, response); MHD_destroy_response (response); if (ret == MHD_NO) abort (); return ret; }
static int answer_to_connection(void *cls, struct MHD_Connection *connection, const char *url, const char *method, const char *version, const char *upload_data, size_t *upload_data_size, void **con_cls) { const char *page = "<html><body>Hello, World!</body></html>"; struct MHD_Response *response; int ret; fprintf(stderr, "respond to %s %s\n", method, url); response = MHD_create_response_from_buffer(strlen(page), (void *)page, MHD_RESPMEM_PERSISTENT); ret = MHD_queue_response(connection, MHD_HTTP_OK, response); MHD_destroy_response(response); return ret; }
static void _put_send_response(struct MHD_Connection *connection, mhd_request_t *request) { const char *page = "<html><body>Dummy body</body></html>"; struct MHD_Response *response; GHashTableIter iter; gpointer key, value; int ret; /* Send a dummy response to client */ response = MHD_create_response_from_buffer(strlen (page), (void*) page, MHD_RESPMEM_PERSISTENT); g_assert(response != NULL && "MHD internal error"); g_hash_table_iter_init(&iter, request->resp_headers); while (g_hash_table_iter_next(&iter, &key, &value)) { ret = MHD_add_response_header(response, key, value); g_assert(ret == MHD_YES && "MHD internal error"); } ret = MHD_queue_response(connection, request->return_code, response); g_assert(ret == MHD_YES && "MHD internal error"); MHD_destroy_response(response); }
static int ahc_echo (void *cls, struct MHD_Connection *connection, const char *url, const char *method, const char *version, const char *upload_data, size_t *upload_data_size, void **unused) { static int ptr; const char *me = cls; struct MHD_Response *response; int ret; (void)version;(void)upload_data;(void)upload_data_size; /* Unused. Silent compiler warning. */ if (0 != strcmp (me, method)) return MHD_NO; /* unexpected method */ if (&ptr != *unused) { *unused = &ptr; return MHD_YES; } *unused = NULL; response = MHD_create_response_from_buffer (strlen (url), (void *) url, MHD_RESPMEM_MUST_COPY); ret = MHD_queue_response (connection, MHD_HTTP_OK, response); MHD_destroy_response (response); if (ret == MHD_NO) abort (); return ret; }
int main (int argc, char *const *argv) { unsigned int errorCount = 0; int port = 1081; oneone = NULL != strstr (argv[0], "11"); if (0 != curl_global_init (CURL_GLOBAL_WIN32)) return 2; response = MHD_create_response_from_buffer (strlen ("/hello_world"), "/hello_world", MHD_RESPMEM_MUST_COPY); errorCount += testInternalGet (port++, 0); errorCount += testMultithreadedGet (port++, 0); errorCount += testMultithreadedPoolGet (port++, 0); errorCount += testExternalGet (port++); #ifndef WINDOWS errorCount += testInternalGet (port++, MHD_USE_POLL); errorCount += testMultithreadedGet (port++, MHD_USE_POLL); errorCount += testMultithreadedPoolGet (port++, MHD_USE_POLL); #endif #if EPOLL_SUPPORT errorCount += testInternalGet (port++, MHD_USE_EPOLL_LINUX_ONLY); errorCount += testMultithreadedPoolGet (port++, MHD_USE_EPOLL_LINUX_ONLY); #endif MHD_destroy_response (response); if (errorCount != 0) fprintf (stderr, "Error (code: %u)\n", errorCount); curl_global_cleanup (); return errorCount != 0; /* 0 == pass */ }
static int connection_handler (void *cls, struct MHD_Connection *connection, const char *url, const char *method, const char *version, const char *upload_data, size_t * upload_data_size, void **ptr) { static int i; if (*ptr == NULL) { *ptr = &i; return MHD_YES; } if (*upload_data_size != 0) { (*upload_data_size) = 0; return MHD_YES; } struct MHD_Response *response = MHD_create_response_from_buffer (strlen ("Response"), "Response", MHD_RESPMEM_PERSISTENT); int ret = MHD_queue_response (connection, MHD_HTTP_OK, response); MHD_destroy_response (response); return ret; }
/** * Handler that adds the 'v1' and 'v2' values to the given HTML code. * * @param cls a 'const char *' with the HTML webpage to return * @param mime mime type to use * @param session session handle * @param connection connection to use */ static int fill_v1_v2_form (const void *cls, const char *mime, struct Session *session, struct MHD_Connection *connection) { int ret; const char *form = cls; char *reply; struct MHD_Response *response; reply = malloc (strlen (form) + strlen (session->value_1) + strlen (session->value_2) + 1); snprintf (reply, strlen (form) + strlen (session->value_1) + strlen (session->value_2) + 1, form, session->value_1); /* return static form */ response = MHD_create_response_from_buffer (strlen (reply), (void *) reply, MHD_RESPMEM_MUST_FREE); add_session_cookie (session, response); MHD_add_response_header (response, MHD_HTTP_HEADER_CONTENT_ENCODING, mime); ret = MHD_queue_response (connection, MHD_HTTP_OK, response); MHD_destroy_response (response); return ret; }
static int answer_to_connection (void *cls, struct MHD_Connection *connection, const char *url, const char *method, const char *version, const char *upload_data, size_t *upload_data_size, void **con_cls) { char *user; char *pass; int fail; int ret; struct MHD_Response *response; if (0 != strcmp (method, "GET")) return MHD_NO; if (NULL == *con_cls) { *con_cls = connection; return MHD_YES; } pass = NULL; user = MHD_basic_auth_get_username_password (connection, &pass); fail = ( (user == NULL) || (0 != strcmp (user, "root")) || (0 != strcmp (pass, "pa$$w0rd") ) ); if (user != NULL) free (user); if (pass != NULL) free (pass); if (fail) { const char *page = "<html><body>Go away.</body></html>"; response = MHD_create_response_from_buffer (strlen (page), (void *) page, MHD_RESPMEM_PERSISTENT); ret = MHD_queue_basic_auth_fail_response (connection, "my realm", response); } else { const char *page = "<html><body>A secret.</body></html>"; response = MHD_create_response_from_buffer (strlen (page), (void *) page, MHD_RESPMEM_PERSISTENT); ret = MHD_queue_response (connection, MHD_HTTP_OK, response); } MHD_destroy_response (response); return ret; }
int SendError(MHD_Connection* connection, const char* msg) { MHD_Response* response = MHD_create_response_from_buffer(strlen(msg), (void*) msg, MHD_RESPMEM_PERSISTENT); int ret = MHD_queue_response(connection, 500, response); MHD_destroy_response(response); return ret; }
//RESPONSE void http_response::get_raw_response_str(MHD_Response** response, webserver* ws) { size_t size = &(*content.end()) - &(*content.begin()); *response = MHD_create_response_from_buffer( size, (void*) content.c_str(), MHD_RESPMEM_PERSISTENT ); }
int file_request( void *cls, struct MHD_Connection *connection, const char *url, const char *method, const char *version, const char *upload_data, size_t *upload_data_size, void **con_cls) { static int aptr; struct MHD_Response *response; int ret; FILE *file; struct stat buf; if (0 != strcmp(method, MHD_HTTP_METHOD_GET)) { return MHD_NO; /* unexpected method */ } if (&aptr != *con_cls) { /* do never respond on first call */ *con_cls = &aptr; return MHD_YES; } *con_cls = NULL; /* reset when done */ std::string surl(&url[1]); const char* filepath = (path + surl).c_str(); if (0 == stat(filepath, &buf)) { file = fopen(filepath, "rb"); } else { file = NULL; } if (file == NULL) { response = MHD_create_response_from_buffer(strlen(filepath), (void *)filepath, MHD_RESPMEM_PERSISTENT); ret = MHD_queue_response(connection, MHD_HTTP_NOT_FOUND, response); MHD_destroy_response(response); } else { /* 32k page size */ response = MHD_create_response_from_callback( buf.st_size, 32 * 1024, &file_reader, file, &free_callback); if (response == NULL) { fclose(file); return MHD_NO; } ret = MHD_queue_response(connection, MHD_HTTP_OK, response); MHD_destroy_response(response); } return ret; }