/** * Handler for the index request. */ static int request_index(struct http_server_connection_t *connection_p, struct http_server_request_t *request_p) { static const char index_html[] = "<!DOCTYPE HTML>\n" "<html>\n" " <body>\n" " Hello from Simba!" " </body>\n" "</html>\n"; struct http_server_response_t response; /* Only the GET action is supported. */ if (request_p->action != http_server_request_action_get_t) { return (-1); } /* Create the response. */ response.code = http_server_response_code_200_ok_t; response.content.type = http_server_content_type_text_html_t; response.content.buf_p = index_html; response.content.size = strlen(response.content.buf_p); return (http_server_response_write(connection_p, request_p, &response)); }
/** * Default page handler. */ static int no_route(struct http_server_connection_t *connection_p, struct http_server_request_t *request_p) { struct http_server_response_t response; /* Create the response. */ response.code = http_server_response_code_404_not_found_t; response.content.type = http_server_content_type_text_html_t; response.content.buf_p = NULL; response.content.size = 0; return (http_server_response_write(connection_p, request_p, &response)); }
int on_message_complete(http_server_client * client, void * data) { char * url; int r = http_server_client_getinfo(client, HTTP_SERVER_CLIENTINFO_URL, &url); assert(r == HTTP_SERVER_OK); fprintf(stderr, "Message complete %s\n", url); http_server_response * res = http_server_response_new(); assert(res); r = http_server_response_begin(client, res); assert(r == HTTP_SERVER_OK); r = http_server_response_write_head(res, 200); assert(r == HTTP_SERVER_OK); char chunk[1024]; int len = sprintf(chunk, "Hello world!\n"); r = http_server_response_write(res, chunk, len); assert(r == HTTP_SERVER_OK); r = http_server_response_end(res); assert(r == HTTP_SERVER_OK); return 0; }