src/HTTPServer.cpp
branchv_0
changeset 4 37a86904145c
parent 3 1184f3de5533
child 5 121981e6bd54
equal deleted inserted replaced
3:1184f3de5533 4:37a86904145c
    52 	MHD_AccessHandlerCallback accessCallback = [](void* rawImpl, 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) {
    52 	MHD_AccessHandlerCallback accessCallback = [](void* rawImpl, 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) {
    53 		HTTPServer::HTTPServerImpl* impl = static_cast<HTTPServer::HTTPServerImpl*> (rawImpl);
    53 		HTTPServer::HTTPServerImpl* impl = static_cast<HTTPServer::HTTPServerImpl*> (rawImpl);
    54 		// TODO: return also HTTP headers
    54 		// TODO: return also HTTP headers
    55 		if (impl->requestHandler) {
    55 		if (impl->requestHandler) {
    56 			HTTPServer::Request request;
    56 			HTTPServer::Request request;
       
    57 			request.method = method;
       
    58 			request.url = url;
       
    59 			// FIXME: request.body = ...
       
    60 			// FIXME: multiple calls for one request
    57 			const HTTPServer::Response response = impl->requestHandler->handle(request);
    61 			const HTTPServer::Response response = impl->requestHandler->handle(request);
    58 			struct MHD_Response* mhdResponse = MHD_create_response_from_buffer(response.body.size(), (void*) response.body.c_str(), MHD_RESPMEM_MUST_COPY);
    62 			struct MHD_Response* mhdResponse = MHD_create_response_from_buffer(response.body.size(), (void*) response.body.c_str(), MHD_RESPMEM_MUST_COPY);
       
    63 			for (Header h : response.header) MHD_add_response_header(mhdResponse, h.name.c_str(), h.value.c_str());
    59 			MHD_queue_response(connection, response.code, mhdResponse);
    64 			MHD_queue_response(connection, response.code, mhdResponse);
    60 			MHD_destroy_response(mhdResponse);
    65 			MHD_destroy_response(mhdResponse);
    61 			return MHD_YES;
    66 			return MHD_YES;
    62 		} else {
    67 		} else {
    63 			// there might be a point in time when the HTTP server is started and HTTP handler is not set
    68 			// there might be a point in time when the HTTP server is started and HTTP handler is not set
    64 			// TODO: just return MHD_NO?
    69 			// TODO: just return MHD_NO?
    65 			static const char body[] = "<h1>HTTP 404: Not found</h1>";
    70 			static const char body[] = "<h1>HTTP 503: Service Unavailable</h1><p>not fully started yet</p>";
    66 			struct MHD_Response* mhdResponse = MHD_create_response_from_buffer(sizeof (body), (void*) body, MHD_RESPMEM_PERSISTENT);
    71 			struct MHD_Response* mhdResponse = MHD_create_response_from_buffer(sizeof (body), (void*) body, MHD_RESPMEM_PERSISTENT);
    67 			MHD_queue_response(connection, 404, mhdResponse);
    72 			MHD_queue_response(connection, 503, mhdResponse);
    68 			MHD_destroy_response(mhdResponse);
    73 			MHD_destroy_response(mhdResponse);
    69 			return MHD_YES;
    74 			return MHD_YES;
    70 		}
    75 		}
    71 	};
    76 	};
    72 
    77