void* remoteConfigThread(void* param) { RemoteConfigImpl* self = (RemoteConfigImpl*)param; while(!self->base.requestExit) { struct timeval to = {0, 1000}; if (httpdGetConnection(self->base.http, &to) <= 0) continue; if(httpdReadRequest(self->base.http) < 0) { httpdEndRequest(self->base.http); continue; } remoteConfigLockImpl(self); httpdProcessRequest(self->base.http); remoteConfigUnlockImpl(self); httpdEndRequest(self->base.http); } return nullptr; }
int main( int argc, char *argv[]) { httpd *server; /* ** Create a server and setup our logging */ server = httpdCreate(NULL,1234); if (server == NULL) { perror("Can't create server"); exit(1); } httpdSetAccessLog(server, stdout); httpdSetErrorLog(server, stderr); /* ** Setup some content for the server */ httpdAddCContent(server,"/", "dummy", HTTP_TRUE, NULL, dummy_html); httpdSetFileBase(server, "/home/prabinb/queralyzer/queralyzerUI/"); httpdAddFileContent(server, "/", "index.html", HTTP_TRUE, NULL, "index.html" ); httpdAddWildcardContent(server, "/css", NULL, "css"); httpdAddWildcardContent(server, "/img", NULL, "img"); httpdAddWildcardContent(server, "/js", NULL, "js"); httpdAddWildcardContent(server, "/js/lib", NULL, "js/lib"); httpdAddWildcardContent(server, "/bootstrap", NULL, "bootstrap"); httpdAddWildcardContent(server, "/bootstrap/css", NULL, "bootstrap/css"); httpdAddWildcardContent(server, "/bootstrap/img", NULL, "bootstrap/img"); httpdAddWildcardContent(server, "/bootstrap/js", NULL, "bootstrap/js"); httpdAddCContent(server,"/", "query", HTTP_TRUE, NULL, query_html); httpdAddCContent(server,"/", "tablemetadata", HTTP_TRUE, NULL, table_data_html); httpdAddCContent(server,"/", "indexmetadata", HTTP_TRUE, NULL, index_data_html); /* ** Go into our service loop */ while(1 == 1) { if (httpdGetConnection(server, 0) < 0) continue; if(httpdReadRequest(server) < 0) { httpdEndRequest(server); continue; } else { httpdProcessRequest(server); httpdEndRequest(server); } } }
/** Main request handling thread. @param args Two item array of void-cast pointers to the httpd and request struct */ void thread_httpd(void *args) { void **params; httpd *webserver; request *r; params = (void **)args; webserver = *params; r = *(params + 1); free(params); /* XXX We must release this ourselves. */ if (httpdReadRequest(webserver, r) == 0) { /* * We read the request fine */ debug(LOG_DEBUG, "Processing request from %s", r->clientAddr); debug(LOG_DEBUG, "Calling httpdProcessRequest() for %s", r->clientAddr); //前面有通过函数httpdAddCContent设置访问页面的回调函数..如果请求的不是页面没有设置, //那就会调用404错误... //该函数httpdSetErrorFunction会设定404错误的回调函数. httpdProcessRequest(webserver, r); debug(LOG_DEBUG, "Returned from httpdProcessRequest() for %s", r->clientAddr); } else { debug(LOG_DEBUG, "No valid request received from %s", r->clientAddr); } debug(LOG_DEBUG, "Closing connection with %s", r->clientAddr); httpdEndRequest(r); }
/**@internal * Main execution loop */ static void main_loop(void) { int result; pthread_t tid; s_config *config = config_get_config(); request *r; void **params; /* Set the time when wifidog 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. Can't fail. */ 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); } debug(LOG_DEBUG, "%s = %s", config->gw_interface, config->gw_address); } /* If we don't have the Gateway ID, construct it from the internal MAC address. * "Can't fail" so exit() if the impossible happens. */ if (!config->gw_id) { debug(LOG_DEBUG, "Finding MAC address of %s", config->gw_interface); if ((config->gw_id = 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_DEBUG, "%s = %s", config->gw_interface, config->gw_id); } /* Initializes the web server */ debug(LOG_NOTICE, "Creating web server on %s:%d", config->gw_address, config->gw_port); if ((webserver = httpdCreate(config->gw_address, config->gw_port)) == NULL) { debug(LOG_ERR, "Could not create web server: %s", strerror(errno)); exit(1); } register_fd_cleanup_on_fork(webserver->serverSock); debug(LOG_DEBUG, "Assigning callbacks to web server"); httpdAddCContent(webserver, "/", "wifidog", 0, NULL, http_callback_wifidog); httpdAddCContent(webserver, "/wifidog", "", 0, NULL, http_callback_wifidog); httpdAddCContent(webserver, "/wifidog", "about", 0, NULL, http_callback_about); httpdAddCContent(webserver, "/wifidog", "status", 0, NULL, http_callback_status); httpdAddCContent(webserver, "/wifidog", "auth", 0, NULL, http_callback_auth); httpdAddCContent(webserver, "/wifidog", "disconnect", 0, NULL, http_callback_disconnect); httpdSetErrorFunction(webserver, 404, http_callback_404); /* Reset the firewall (if WiFiDog crashed) */ fw_destroy(); /* Then initialize it */ if (!fw_init()) { debug(LOG_ERR, "FATAL: Failed to initialize firewall"); exit(1); } /* Start clean up thread */ result = pthread_create(&tid_fw_counter, NULL, (void *)thread_client_timeout_check, NULL); if (result != 0) { debug(LOG_ERR, "FATAL: Failed to create a new thread (fw_counter) - exiting"); termination_handler(0); } pthread_detach(tid_fw_counter); /* Start control thread */ result = pthread_create(&tid, NULL, (void *)thread_wdctl, (void *)safe_strdup(config->wdctl_sock)); if (result != 0) { debug(LOG_ERR, "FATAL: Failed to create a new thread (wdctl) - exiting"); termination_handler(0); } pthread_detach(tid); /* Start heartbeat thread */ result = pthread_create(&tid_ping, NULL, (void *)thread_ping, NULL); if (result != 0) { debug(LOG_ERR, "FATAL: Failed to create a new thread (ping) - exiting"); termination_handler(0); } pthread_detach(tid_ping); 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 */ if (NULL != r) { httpdEndRequest(r); } } 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 * * We should create another thread */ debug(LOG_INFO, "Received connection from %s, spawning worker thread", r->clientAddr); /* The void**'s are a simulation of the normal C * function calling sequence. */ params = safe_malloc(2 * sizeof(void *)); *params = webserver; *(params + 1) = r; result = pthread_create(&tid, NULL, (void *)thread_httpd, (void *)params); if (result != 0) { debug(LOG_ERR, "FATAL: Failed to create a new thread (httpd) - exiting"); termination_handler(0); } pthread_detach(tid); } else { /* webserver->lastError should be 2 */ /* XXX We failed an ACL.... No handling because * we don't set any... */ } } /* never reached */ }