void Server::commandHandler() { uint32_t command; try { command = recvLong(); } catch (std::exception &e) { disconnect(); return; } try { if (command == StupidsClient::CMD_EXIT) { flushToClient(); disconnect(); return; } else if (command == StupidsClient::CMD_GET_MIN_ID_ARRAY) handleGetMinIdArray(); else if (command == StupidsClient::CMD_GET_FIRST_ID) handleGetFirstId(); else if (command == StupidsClient::CMD_GET_FIRST_ID_MULTI) handleGetFirstIdMulti(); else if (command == StupidsClient::CMD_GET_MIN_IDS) handleGetMinIds(); else if (command == StupidsClient::CMD_INVOLVED_BY_MIN_IDS) handleInvolvedByMinIds(); else { printf("Unknown command code %d.\n", command); flushErrorToClient(1); disconnect(); return; } } catch (std::exception &e) { printf("Exception raised while processing client's requests:\n\t%s\n", e.what()); flushErrorToClient(2); disconnect(); return; } flushToClient(); }
/* Forward the HTTP requesto to another server. \param td The HTTP thread context. \param scriptpath Not used. \param exec The remote server Url. \param execute Not used. \param onlyHeader Specify if send only the HTTP header. */ int Proxy::send (HttpThreadContext *td, const char* scriptpath, const char* exec, bool execute, bool onlyHeader) { Url destUrl (exec, 80); ConnectionPtr con = NULL; Socket *sock; FiltersChain chain; HttpRequestHeader req; size_t nbw; bool keepalive = false; for (HashMap<string, HttpRequestHeader::Entry*>::Iterator it = td->request.begin (); it != td->request.end (); it++) { HttpRequestHeader::Entry *e = *it; req.setValue (e->name.c_str (), e->value.c_str ()); } if (stringcmpi (destUrl.getProtocol (), "http")) { td->connection->host->warningsLogWrite ("Proxy: %s is not a supported protocol", destUrl.getProtocol ().c_str ()); return td->http->raiseHTTPError (500); } try { req.ver.assign ("HTTP/1.1"); req.cmd.assign (td->request.cmd); if (destUrl.getResource ()[0] == '\0' && td->pathInfo[0] == '\0') req.uri = "/"; else { req.uri = destUrl.getResource (); req.uri.append (td->pathInfo); } if (td->request.uriOpts.length ()) { req.uri.append ("?"); req.uri.append (td->request.uriOpts); } req.setValue ("Connection", "keep-alive"); if (td->request.uriOptsPtr) { char buffer[32]; size_t size = td->inputData.getFileSize (); sprintf (buffer, "%u", size); req.setValue ("Content-Length", buffer); } ostringstream host; host << destUrl.getHost (); if (destUrl.getPort () != 80 ) host << ":" << destUrl.getPort (); req.setValue ("Host", host.str ().c_str ()); string xForwardedFor; td->request.getValue ("X-Forwarded-For", &xForwardedFor); if (xForwardedFor.size ()) xForwardedFor.append (", "); xForwardedFor.append (td->connection->getIpAddr ()); req.setValue ("X-Forwarded-For", xForwardedFor.c_str ()); con = getConnection (destUrl.getHost ().c_str (), destUrl.getPort ()); if (! con) return td->http->raiseHTTPError (500); sock = con->socket; u_long hdrLen = HttpHeaders::buildHTTPRequestHeader (td->auxiliaryBuffer->getBuffer (), &req); sock->write (td->auxiliaryBuffer->getBuffer (), hdrLen, &nbw); if (td->request.uriOptsPtr) td->inputData.fastCopyToSocket (sock, 0, td->auxiliaryBuffer, &nbw); chain.setStream (td->connection->socket); if (td->mime) Server::getInstance ()->getFiltersFactory ()->chain (&chain, td->mime->filters, td->connection->socket, &nbw, 1); flushToClient (td, *sock, chain, onlyHeader, &keepalive); chain.clearAllFilters (); addConnection (con, destUrl.getHost ().c_str (), destUrl.getPort (), keepalive); req.free (); } catch (exception & e) { if (con) addConnection (con, destUrl.getHost ().c_str (), destUrl.getPort (), false); chain.clearAllFilters (); return td->http->raiseHTTPError (500); } return HttpDataHandler::RET_OK; }