static void handle_http_header_parsed(Connection *conn) { http_request *req = &conn->req; int content_length = req->content_length; if (content_length < 0) { content_length = 0; } conn->in_body = true; bool keep_alive = http_request_is_keep_alive(req); int num_bytes_in_buffer = conn->read_buffer.size() - req->body_start; // // make sure we complete reading in the body // XXX: Handle Chunked-Encoding // if (content_length <= num_bytes_in_buffer) { // we have read the body completely into the buffer if (!keep_alive) { int err = uv_read_stop((uv_stream_t*) conn); assert(err == 0); } handle_http_request(conn); } }
void client_handler(int client_socket, server_parameters * parameters) { to_log(&logger, "Client handler started: Worker %d\n", getpid()); char buffer[1024]; int read = recv(client_socket, buffer, 1024, MSG_NOSIGNAL); while (1) { if (read == -1 && (errno == EAGAIN || errno == EWOULDBLOCK)) { // should retry continue; } else if (read <= 0) { to_log(&logger, "Worker %d: client discontected\n", getpid()); break; } else { to_log(&logger, "Worker %d: received request: %s", getpid(), buffer); int response_length = 0; char * response = handle_http_request(buffer, read, &response_length, parameters->directory); to_log(&logger, "Worker %d: sending response: %s", getpid(), response); send(client_socket, response, response_length, MSG_NOSIGNAL); free(response); close(client_socket); break; } } exit(0); }
inline void process_input() { char c; cli(); while ( ! input_buffer_.empty()) { c = input_buffer_.front(); input_buffer_.pop_front(); sei(); cmd_buffer_.append(c); switch (session_type_) { case UNDECIDED: if ( c == '\n') { if (is_http_request()) { session_type_ = HTTP_SESSION; handle_http_request(c); } else { session_type_ = CLI_SESSION; handle_cli_session(c); } } break; case CLI_SESSION: handle_cli_session(c); break; case HTTP_SESSION: handle_http_request(c); break; } cli(); } sei(); }
/**@internal * Main execution loop */ static void main_loop(void) { int result; pthread_t tid; s_config *config = config_get_config(); struct timespec wait_time; int msec; request *r; void **params; int* thread_serial_num_p; /* Set the time when nodogsplash started */ if (!started_time) { debug(LOG_INFO, "Setting started_time"); started_time = time(NULL); } else if (started_time < MINIMUM_STARTED_TIME) { debug(LOG_WARNING, "Detected possible clock skew - re-setting started_time"); started_time = time(NULL); } /* If we don't have the Gateway IP address, get it. Exit on failure. */ if (!config->gw_address) { debug(LOG_DEBUG, "Finding IP address of %s", config->gw_interface); if ((config->gw_address = get_iface_ip(config->gw_interface)) == NULL) { debug(LOG_ERR, "Could not get IP address information of %s, exiting...", config->gw_interface); exit(1); } if ((config->gw_mac = get_iface_mac(config->gw_interface)) == NULL) { debug(LOG_ERR, "Could not get MAC address information of %s, exiting...", config->gw_interface); exit(1); } debug(LOG_NOTICE, "Detected gateway %s at %s (%s)", config->gw_interface, config->gw_address, config->gw_mac); } /* Initializes the web server */ if ((webserver = httpdCreate(config->gw_address, config->gw_port)) == NULL) { debug(LOG_ERR, "Could not create web server: %s", strerror(errno)); exit(1); } debug(LOG_NOTICE, "Created web server on %s:%d", config->gw_address, config->gw_port); /* Set web root for server */ debug(LOG_DEBUG, "Setting web root: %s",config->webroot); httpdSetFileBase(webserver,config->webroot); /* Add images files to server: any file in config->imagesdir can be served */ debug(LOG_DEBUG, "Setting images subdir: %s",config->imagesdir); httpdAddWildcardContent(webserver,config->imagesdir,NULL,config->imagesdir); /* Add pages files to server: any file in config->pagesdir can be served */ debug(LOG_DEBUG, "Setting pages subdir: %s",config->pagesdir); httpdAddWildcardContent(webserver,config->pagesdir,NULL,config->pagesdir); debug(LOG_DEBUG, "Registering callbacks to web server"); httpdAddCContent(webserver, "/", "", 0, NULL, http_nodogsplash_callback_index); httpdAddCWildcardContent(webserver, config->authdir, NULL, http_nodogsplash_callback_auth); httpdAddCWildcardContent(webserver, config->denydir, NULL, http_nodogsplash_callback_deny); httpdAddC404Content(webserver, http_nodogsplash_callback_404); /* Reset the firewall (cleans it, in case we are restarting after nodogsplash crash) */ fw_destroy(); /* Then initialize it */ debug(LOG_NOTICE, "Initializing firewall rules"); if( fw_init() != 0 ) { debug(LOG_ERR, "Error initializing firewall rules! Cleaning up"); fw_destroy(); debug(LOG_ERR, "Exiting because of error initializing firewall rules"); exit(1); } /* Start client statistics and timeout clean-up thread */ result = pthread_create(&tid_client_check, NULL, (void *)thread_client_timeout_check, NULL); if (result != 0) { debug(LOG_ERR, "FATAL: Failed to create thread_client_timeout_check - exiting"); termination_handler(0); } pthread_detach(tid_client_check); /* Start control thread */ result = pthread_create(&tid, NULL, (void *)thread_ndsctl, (void *)safe_strdup(config->ndsctl_sock)); if (result != 0) { debug(LOG_ERR, "FATAL: Failed to create thread_ndsctl - exiting"); termination_handler(0); } pthread_detach(tid); /* * Enter the httpd request handling loop */ debug(LOG_NOTICE, "Waiting for connections"); while(1) { r = httpdGetConnection(webserver, NULL); /* We can't convert this to a switch because there might be * values that are not -1, 0 or 1. */ if (webserver->lastError == -1) { /* Interrupted system call */ continue; /* continue loop from the top */ } else if (webserver->lastError < -1) { /* * FIXME * An error occurred - should we abort? * reboot the device ? */ debug(LOG_ERR, "FATAL: httpdGetConnection returned unexpected value %d, exiting.", webserver->lastError); termination_handler(0); } else if (r != NULL) { /* We got a connection */ handle_http_request(webserver, r); } else { /* webserver->lastError should be 2 */ /* XXX We failed an ACL.... No handling because * we don't set any... */ } } /* never reached */ }