/** * @brief serve_file try to serve a request via filesystem. Using webroot as root. * @param connection * @param client * @return */ static int serve_file(struct MHD_Connection *connection, t_client *client, const char *url) { struct stat stat_buf; s_config *config = config_get_config(); struct MHD_Response *response; char filename[PATH_MAX]; int ret = MHD_NO; const char *mimetype = NULL; off_t size; snprintf(filename, PATH_MAX, "%s/%s", config->webroot, url); /* check if file exists and is not a directory */ ret = stat(filename, &stat_buf); if (ret) { /* stat failed */ return send_error(connection, 404); } if (!S_ISREG(stat_buf.st_mode)) { #ifdef S_ISLNK /* ignore links */ if (!S_ISLNK(stat_buf.st_mode)) #endif /* S_ISLNK */ return send_error(connection, 404); } int fd = open(filename, O_RDONLY); if (fd < 0) return send_error(connection, 404); mimetype = lookup_mimetype(filename); /* serving file and creating response */ size = lseek(fd, 0, SEEK_END); if (size < 0) return send_error(connection, 404); response = MHD_create_response_from_fd(size, fd); if (!response) return send_error(connection, 503); MHD_add_response_header(response, "Content-Type", mimetype); ret = MHD_queue_response(connection, MHD_HTTP_OK, response); MHD_destroy_response(response); return ret; }
int test_http_server_ok_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) { test_http_server * handle = cls; struct MHD_Response * response; int ret; int * c_count; if (*ptr == NULL) { handle->log = apr_hash_make(handle->pool); /* The first time only the headers are valid, do not respond in the first round... */ *ptr = cls; //just to flag its not null.... int * c_count = apr_palloc(handle->pool, sizeof(int)); *c_count = 0; *ptr = c_count; return MHD_YES; } c_count = *ptr; if(*c_count == 0) { //log the request.... apr_hash_set(handle->log, apr_pstrdup(handle->pool, "METHOD"), APR_HASH_KEY_STRING, apr_pstrdup(handle->pool, method)); apr_hash_set(handle->log, apr_pstrdup(handle->pool, "URL"), APR_HASH_KEY_STRING, apr_pstrdup(handle->pool, url)); apr_hash_set(handle->log, apr_pstrdup(handle->pool, "VERSION"), APR_HASH_KEY_STRING, apr_pstrdup(handle->pool, version)); apr_hash_set(handle->log, apr_pstrdup(handle->pool, "DATA_LENGTH"), APR_HASH_KEY_STRING, apr_pmemdup (handle->pool, upload_data_size, sizeof(size_t))); apr_hash_set(handle->log, apr_pstrdup(handle->pool, "DATA"), APR_HASH_KEY_STRING, apr_pmemdup (handle->pool, upload_data, *upload_data_size)); } *c_count = *c_count+1; if(*upload_data_size == 0) { response = MHD_create_response_from_data(strlen(handle->response), (void*) handle->response, MHD_NO, MHD_NO); ret = MHD_queue_response(connection, handle->response_code, response); MHD_destroy_response(response); return ret; } else { *upload_data_size = 0; //we processed everything? return MHD_YES; } }
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); } 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; }
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; const char *hdr; if (0 != strcmp (me, method)) return MHD_NO; /* unexpected method */ if (&ptr != *unused) { *unused = &ptr; return MHD_YES; } *unused = NULL; hdr = MHD_lookup_connection_value (connection, MHD_GET_ARGUMENT_KIND, "k"); if ((hdr == NULL) || (0 != strcmp (hdr, "v x"))) abort (); hdr = MHD_lookup_connection_value (connection, MHD_GET_ARGUMENT_KIND, "hash"); if ((hdr == NULL) || (0 != strcmp (hdr, "#foo"))) abort (); hdr = MHD_lookup_connection_value (connection, MHD_GET_ARGUMENT_KIND, "space"); if ((hdr == NULL) || (0 != strcmp (hdr, "\240bar"))) abort (); if (3 != MHD_get_connection_values (connection, MHD_GET_ARGUMENT_KIND, NULL, NULL)) abort (); 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 ahc_echo(void* servctx, MHD_Connection* connection, const char* url, const char* method, const char* version, const char* upload_data, size_t* upload_data_size, void** reqctx) { static int dummy; if (reqctx == NULL) { // The first time only the headers are valid, do not respond in the first round. *reqctx = &dummy; return MHD_YES; } ServerContext* pServCtx = (ServerContext*)servctx; // Not used yet printf("%s %s %s\n", method, url, version); if (strcmp(method, "GET") == 0) { if (*upload_data_size != 0) return MHD_NO; // No upload data expected in HTTP GET method // Parse headers puts("[HEADERS]"); MHD_get_connection_values(connection, MHD_HEADER_KIND, &print_out_key, NULL); // Parse GET parameters puts("[GET PARAMS]"); StringPairList spl; MHD_get_connection_values(connection, MHD_GET_ARGUMENT_KIND, &print_out_key, &spl); std::string text = "<html><body>\n"; text += "<p>URL="; text += url; text += "</p>\n<p>Parameters</p>\n<ul>\n"; for (StringPairList::iterator it = spl.begin(); it != spl.end(); ++it) { text += "<li>"; text += it->first; text += '='; text += it->second; text += "</li>\n"; } text += "</body></html>\n"; MHD_Response* response = MHD_create_response_from_buffer(text.size(), (void*)text.c_str(), MHD_RESPMEM_MUST_COPY); MHD_add_response_header(response, "Content-Type", "text/html"); int ret = MHD_queue_response(connection, MHD_HTTP_OK, response); MHD_destroy_response(response); *reqctx = NULL; // clear context pointer return ret; } else return MHD_NO; // Not supported yet }
/** * Handler that adds the 'v1' value to the given HTML code. * * @param cls unused * @param mime mime type to use * @param session session handle * @param connection connection to use */ static int ServeMainPage (const void *cls, const char *mime, struct Session *session, struct MHD_Connection *connection) { int ret; char *reply; struct MHD_Response *response; reply = malloc(MAXTEXTFILELENGTH); if (NULL == reply) return MHD_NO; if ( !ReadTextFileIntoString(reply, "data/MainPage.html") ) return MHD_NO; /* // Open the MainPage html file FILE *fp = fopen("data/MainPage.html", "rb"); if (NULL == fp) return MHD_NO; fseek(fp, 0L, SEEK_END); long sz = ftell(fp); fseek(fp, 0L, SEEK_SET); reply = malloc (sz+1); if (NULL == reply) return MHD_NO; fread (reply, 1, sz, fp); fclose (fp); */ /* return static form */ response = MHD_create_response_from_buffer (strlen (reply), (void *) reply, MHD_RESPMEM_MUST_FREE); 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; }
void http_create_response(struct Proxy* proxy, char **nv) { size_t i; if(!proxy->http_active) return; for(i = 0; nv[i]; i += 2) { if(0 == strcmp(":status", nv[i])) { char tmp[4]; memcpy(&tmp,nv[i+1],3); tmp[3]=0; proxy->status = atoi(tmp); continue; } else if(0 == strcmp(":version", nv[i])) { proxy->version = nv[i+1]; continue; } else if(0 == strcmp("content-length", nv[i])) { continue; } char *header = *(nv+i); if(MHD_NO == MHD_add_response_header (proxy->http_response, header, nv[i+1])) { PRINT_INFO2("SPDY_name_value_add failed: '%s' '%s'", header, nv[i+1]); } PRINT_INFO2("adding '%s: %s'",header, nv[i+1]); } if(MHD_NO == MHD_queue_response (proxy->http_connection, proxy->status, proxy->http_response)){ PRINT_INFO("No queue"); //TODO //abort(); proxy->http_error = true; } MHD_destroy_response (proxy->http_response); proxy->http_response = NULL; }
inline void set_auth_cookie(struct MHD_Connection *connection) { char response[64] = { 0 }; char cookie[256] = { 0 }; sprintf(response, "{\"login\": \"YES\"}"); last_cookie = get_random_cookie(); sprintf(cookie, AUTHENTICATE_COOKIE_FORAMT, AUTHENTICATE_COOKIE_NAME, last_cookie, AUTHENTICATE_COOKIE_TIME); response = MHD_create_response_from_buffer (strlen(login), (void *)(login), MHD_RESPMEM_PERSISTENT); MHD_add_response_header (response, MHD_HTTP_HEADER_SET_COOKIE, set_coockie); MHD_queue_response (connection, MHD_HTTP_OK, response); MHD_destroy_response (response); }
int HTTPServer::send_page (struct MHD_Connection *connection, const char *page) { int ret; struct MHD_Response *response; response = MHD_create_response_from_buffer (strlen (page), (void *) page, MHD_RESPMEM_PERSISTENT); if (!response) return MHD_NO; ret = MHD_queue_response (connection, MHD_HTTP_OK, response); MHD_destroy_response (response); return ret; }
// return MHD_NO or MHD_YES virtual int handleRequest( struct MHD_Connection *connection, const char */*url*/, const char *method, const char */*version*/, const char *upload_data, size_t *upload_data_size) { // new request if(mState == BEGIN) { if(strcmp(method, "POST") == 0) { mState = WAITING_DATA; // first time there is no data, do nothing and return return MHD_YES; } } if(mState == WAITING_DATA) { if(upload_data && *upload_data_size) { mRequesString += std::string(upload_data, *upload_data_size); *upload_data_size = 0; return MHD_YES; } } std::vector<uint8_t> bytes(mRequesString.begin(), mRequesString.end()); int id = mApiServer->getTmpBlobStore()->storeBlob(bytes); resource_api::JsonStream responseStream; if(id) responseStream << makeKeyValue("ok", true); else responseStream << makeKeyValue("ok", false); responseStream << makeKeyValueReference("id", id); std::string result = responseStream.getJsonString(); struct MHD_Response* resp = MHD_create_response_from_data(result.size(), (void*)result.data(), 0, 1); MHD_add_response_header(resp, "Content-Type", "application/json"); secure_queue_response(connection, MHD_HTTP_OK, resp); MHD_destroy_response(resp); return MHD_YES; }
static int send_bad_response(struct MHD_Connection *connection) { static char *bad_response = (char *) ERROR_PAGE; int bad_response_len = strlen(bad_response); int ret; struct MHD_Response *response; response = MHD_create_response_from_buffer(bad_response_len, bad_response, MHD_RESPMEM_PERSISTENT); if (response == 0) { return MHD_NO; } 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) { const char *page = "<html><body>Hello!</body><html>"; struct MHD_Response *response; int ret; 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 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) { int *done = cls; struct MHD_Response *response; int ret; int have; if (0 != strcmp ("PUT", method)) return MHD_NO; /* unexpected method */ if ((*done) < 8) { have = *upload_data_size; if (have + *done > 8) { printf ("Invalid upload data `%8s'!\n", upload_data); return MHD_NO; } if (0 == memcmp (upload_data, &"Hello123"[*done], have)) { *done += have; *upload_data_size = 0; } else { printf ("Invalid upload data `%8s'!\n", upload_data); return MHD_NO; } #if 0 fprintf (stderr, "Not ready for response: %u/%u\n", *done, 8); #endif return MHD_YES; } 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); 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 **unused) { static int ptr; const char *me = cls; struct MHD_Response *response; int ret; const char *hdr; if (0 != strcmp (me, method)) return MHD_NO; /* unexpected method */ if (&ptr != *unused) { *unused = &ptr; return MHD_YES; } *unused = NULL; ret = 0; hdr = MHD_lookup_connection_value (connection, MHD_COOKIE_KIND, "name1"); if ((hdr == NULL) || (0 != strcmp (hdr, "var1"))) abort (); hdr = MHD_lookup_connection_value (connection, MHD_COOKIE_KIND, "name2"); if ((hdr == NULL) || (0 != strcmp (hdr, "var2"))) abort (); hdr = MHD_lookup_connection_value (connection, MHD_COOKIE_KIND, "name3"); if ((hdr == NULL) || (0 != strcmp (hdr, ""))) abort (); hdr = MHD_lookup_connection_value (connection, MHD_COOKIE_KIND, "name4"); if ((hdr == NULL) || (0 != strcmp (hdr, "var4 with spaces"))) abort (); response = MHD_create_response_from_buffer (strlen (url), (void *) url, MHD_RESPMEM_PERSISTENT); ret = MHD_queue_response (connection, MHD_HTTP_OK, response); MHD_destroy_response (response); if (ret == MHD_NO) abort (); return ret; }
/* * HTTP access handler call back * used to query negotiated security parameters */ static int query_session_ahc (void *cls, struct MHD_Connection *connection, const char *url, const char *method, const char *upload_data, const char *version, size_t *upload_data_size, void **ptr) { struct MHD_Response *response; int ret; if (NULL == *ptr) { *ptr = &query_session_ahc; return MHD_YES; } /* assert actual connection cipher is the one negotiated */ if (GNUTLS_CIPHER_AES_256_CBC != (ret = MHD_get_connection_info (connection, MHD_CONNECTION_INFO_CIPHER_ALGO)->cipher_algorithm)) { fprintf (stderr, "Error: requested cipher mismatch (wanted %d, got %d)\n", GNUTLS_CIPHER_AES_256_CBC, ret); return -1; } if (GNUTLS_SSL3 != (ret = MHD_get_connection_info (connection, MHD_CONNECTION_INFO_PROTOCOL)->protocol)) { fprintf (stderr, "Error: requested protocol mismatch (wanted %d, got %d)\n", GNUTLS_SSL3, ret); return -1; } response = MHD_create_response_from_data (strlen (EMPTY_PAGE), (void *) EMPTY_PAGE, MHD_NO, MHD_NO); 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 **unused) { int *done = cls; struct MHD_Response *response; int ret; if (0 != strcmp ("PUT", method)) return MHD_NO; /* unexpected method */ if ((*done) == 0) { if (*upload_data_size != PUT_SIZE) { #if 0 fprintf (stderr, "Waiting for more data (%u/%u)...\n", *upload_data_size, PUT_SIZE); #endif return MHD_YES; /* not yet ready */ } if (0 == memcmp (upload_data, put_buffer, PUT_SIZE)) { *upload_data_size = 0; } else { printf ("Invalid upload data!\n"); return MHD_NO; } *done = 1; return MHD_YES; } 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); return ret; }
int handle_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) { const char * json_str; int ret; int num_chans = *(int *) cls; print(2, "Local request received: %s %s %s", NULL, version, method, url); struct MHD_Response *response; struct json_object *json_obj = json_object_new_object(); struct json_object *json_data = json_object_new_object(); for (int i = 0; i < num_chans; i++) { channel_t *ch = &chans[i]; reading_t rd; if (strcmp(url, "/") == 0 || strcmp(ch->uuid, url + 1) == 0) { pthread_mutex_lock(&ch->mutex); /* wait for new data comet-like blocking of HTTP response */ pthread_cond_wait(&ch->condition, &ch->mutex); // TODO use pthread_cond_timedwait() pthread_mutex_unlock(&ch->mutex); struct json_object *json_tuples = api_json_tuples(ch, TRUE); json_object_object_add(json_data, "uuid", json_object_new_string(ch->uuid)); json_object_object_add(json_data, "interval", json_object_new_int(ch->interval)); json_object_object_add(json_data, "tuples", json_tuples); } } json_object_object_add(json_obj, "version", json_object_new_string(VZ_VERSION)); json_object_object_add(json_obj, "generator", json_object_new_string("vzlogger")); json_object_object_add(json_obj, "data", json_data); json_str = json_object_to_json_string(json_obj); response = MHD_create_response_from_data(strlen(json_str), (void *) json_str, FALSE, TRUE); MHD_add_response_header(response, "Content-type", "application/json"); ret = MHD_queue_response(connection, MHD_HTTP_OK, response); MHD_destroy_response (response); return ret; }
FUNCTION PRIVATE DEFINITION static int send_page ( struct MHD_Connection* connection, const Str_t& page, const Str_t& mimetype ) { int ret = 0; struct MHD_Response* response = NULL; response = MHD_create_response_from_data(page.size(), (void*) page.c_str(), MHD_NO, MHD_YES); if (!response) return MHD_NO; MHD_add_response_header (response, "Content-Type", mimetype.c_str()); 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 **unused) { struct MHD_Response *response; int ret; response = MHD_create_response_from_buffer (0, NULL, 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 = 1130; (void)argc; /* Unused. Silent compiler warning. */ if (NULL == argv || 0 == argv[0]) return 99; oneone = has_in_name (argv[0], "11"); if (oneone) port += 15; 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++); if (MHD_YES == MHD_is_feature_supported(MHD_FEATURE_THREADS)) { errorCount += testInternalGet (port++, MHD_USE_AUTO); errorCount += testMultithreadedGet (port++, MHD_USE_AUTO); errorCount += testMultithreadedPoolGet (port++, MHD_USE_AUTO); 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); errorCount += testMultithreadedPoolGet(port++, MHD_USE_EPOLL); } } MHD_destroy_response (response); if (errorCount != 0) fprintf (stderr, "Error (code: %u)\n", errorCount); curl_global_cleanup (); return errorCount != 0; /* 0 == pass */ }
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 eok; struct MHD_Response *response; struct MHD_PostProcessor *pp; int ret; if (0 != strcmp ("POST", method)) { printf ("METHOD: %s\n", method); return MHD_NO; /* unexpected method */ } pp = *unused; if (pp == NULL) { eok = 0; pp = MHD_create_post_processor (connection, 1024, &post_iterator, &eok); if (pp == NULL) abort (); *unused = pp; } MHD_post_process (pp, upload_data, *upload_data_size); if ((eok == 3) && (0 == *upload_data_size)) { 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); MHD_destroy_post_processor (pp); *unused = NULL; return ret; } *upload_data_size = 0; return MHD_YES; }
/** * Handler that returns a simple static HTTP page. * * @param connection connection to use * @return MHD_YES on success */ static int serve_main_page (struct MHD_Connection *connection) { int ret; struct MHD_Response *response; /* return static form */ response = MHD_create_response_from_buffer (strlen (MAIN_PAGE), (void *) MAIN_PAGE, MHD_RESPMEM_PERSISTENT); 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; }
int send_response2(struct http_response *resp) { int ret; struct MHD_Response *MHDresponse; void *data_copy; struct http_header *hdr; data_copy = malloc(resp->ndata); memcpy(data_copy, resp->data, resp->ndata); MHDresponse = MHD_create_response_from_data(resp->ndata, data_copy, MHD_YES, MHD_NO); assert(MHDresponse); for (hdr = resp->headers; hdr; hdr = hdr->next) MHD_add_response_header(MHDresponse, hdr->key, hdr->value); ret = MHD_queue_response (resp->connection, resp->status, MHDresponse); assert(ret == 1); MHD_destroy_response (MHDresponse); return ret; }
int send_response(struct MHD_Connection* conn, const char* body, int status_code) { struct MHD_Response* response; if (body) { response = MHD_create_response_from_data(strlen(body), (void*)body, MHD_NO, MHD_YES); } else { response = MHD_create_response_from_data(0, NULL, MHD_NO, MHD_YES); } if (!response) { return MHD_NO; } int ret = MHD_queue_response(conn, status_code, response); MHD_destroy_response(response); return ret; }
/* * web_send_file * Read files and send them out */ int web_send_file (struct MHD_Connection *conn, const char *filename, const int code, const bool unl) { struct stat buf; FILE *fp; struct MHD_Response *response; const char *p; const char *ct = NULL; int ret; if ((p = strchr(filename, '.')) != NULL) { p++; if (strcmp(p, "xml") == 0) ct = "text/xml"; else if (strcmp(p, "js") == 0) ct = "text/javascript"; } if (stat(filename, &buf) == -1 || ((fp = fopen(filename, "r")) == NULL)) { if (strcmp(p, "xml") == 0) response = MHD_create_response_from_data(0, (void *)"", MHD_NO, MHD_NO); else { int len = strlen(FNF) + strlen(filename) - 1; // len(%s) + 1 for \0 char *s = (char *)malloc(len); if (s == NULL) { fprintf(stderr, "Out of memory FNF\n"); exit(1); } snprintf(s, len, FNF, filename); response = MHD_create_response_from_data(len, (void *)s, MHD_YES, MHD_NO); // free } } else response = MHD_create_response_from_callback(buf.st_size, 32 * 1024, &web_read_file, fp, &web_close_file); if (response == NULL) return MHD_YES; if (ct != NULL) MHD_add_response_header(response, "Content-type", ct); ret = MHD_queue_response(conn, code, response); MHD_destroy_response(response); if (unl) unlink(filename); return ret; }
int main (int argc, char *const *argv) { struct MHD_Daemon *d; unsigned int i; if (argc != 2) { printf ("%s PORT\n", argv[0]); return 1; } response = MHD_create_response_from_buffer (strlen (PAGE), (void *) PAGE, MHD_RESPMEM_PERSISTENT); #if 0 (void) MHD_add_response_header (response, MHD_HTTP_HEADER_CONNECTION, "close"); #endif d = MHD_start_daemon (MHD_USE_SELECT_INTERNALLY | MHD_SUPPRESS_DATE_NO_CLOCK #ifdef EPOLL_SUPPORT | MHD_USE_EPOLL | MHD_USE_EPOLL_TURBO #endif , atoi (argv[1]), NULL, NULL, &ahc_echo, NULL, MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) 120, MHD_OPTION_THREAD_POOL_SIZE, (unsigned int) NUMBER_OF_THREADS, MHD_OPTION_URI_LOG_CALLBACK, &uri_logger_cb, NULL, MHD_OPTION_NOTIFY_COMPLETED, &completed_callback, NULL, MHD_OPTION_CONNECTION_LIMIT, (unsigned int) 1000, MHD_OPTION_END); if (d == NULL) return 1; (void) getc (stdin); MHD_stop_daemon (d); MHD_destroy_response (response); for (i=0;i<SMALL;i++) if (0 != small_deltas[i]) fprintf (stdout, "D: %d %u\n", i, small_deltas[i]); return 0; }
int SendTemplate(MHD_Connection* connection, bool redirect, const char* redirectUrl) { SkString debuggerTemplate = generate_template(SkString(FLAGS_source[0])); MHD_Response* response = MHD_create_response_from_buffer( debuggerTemplate.size(), (void*) const_cast<char*>(debuggerTemplate.c_str()), MHD_RESPMEM_MUST_COPY); MHD_add_response_header (response, "Access-Control-Allow-Origin", "*"); int status = MHD_HTTP_OK; if (redirect) { MHD_add_response_header (response, "Location", redirectUrl); status = MHD_HTTP_SEE_OTHER; } int ret = MHD_queue_response(connection, status, response); MHD_destroy_response(response); return ret; }
/** * process http get request * @param url [request url string] * @param connection [http connect handle] */ inline void process_get_url_requert(const char *url, struct MHD_Connection * connection) { struct MHD_Response *response; int fd, size, type; if (url_to_file(url, &fd, &size, &type)) { response = MHD_create_response_from_fd(size, fd); set_mime(response, type); MHD_queue_response(connection, MHD_HTTP_OK, response); MHD_destroy_response(response); return; } else if (url_to_api(url, connection, false, NULL, 0, NULL)) return; else return_404(connection); }
static int callback(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) { struct callback_closure *cbc = calloc(1, sizeof(struct callback_closure)); struct MHD_Response *r; r = MHD_create_response_from_callback (MHD_SIZE_UNKNOWN, 1024, &called_twice, cbc, &free); MHD_queue_response(connection, MHD_HTTP_OK, r); MHD_destroy_response(r); return MHD_YES; }