int HttpServer::svc(void) { char buf[2048]; buf[2047] = '\0'; // security ACE_Time_Value timeout; timeout.sec(5); ssize_t size = peer().recv(buf, 2040, &timeout); if (size > 5) { try { int startUrlOffset = 5; // get rid of "GET /" from Http request, so skip 5 chars char* stopUrl = ACE_OS::strstr(buf+startUrlOffset, " HTTP"); // detect location of post-URL trailing stuff if(!stopUrl) { throw (CStdString("Malformed http request")); ; } *stopUrl = '\0'; // Remove post-URL trailing stuff CStdString url(buf+startUrlOffset); int queryOffset = url.Find("?"); if (queryOffset > 0) { // Strip beginning of URL in case the command is received as an URL "query" of the form: // http://hostname/service/command?type=ping url = url.Right(url.size() - queryOffset - 1); } CStdString className = UrlSerializer::FindClass(url); ObjectRef objRef = ObjectFactory::GetSingleton()->NewInstance(className); if (objRef.get()) { objRef->DeSerializeUrl(url); ObjectRef response = objRef->Process(); if(response.get() == NULL) { throw (CStdString("Command does not return a response:") + className); } else { DOMImplementation* impl = DOMImplementationRegistry::getDOMImplementation(XStr("Core").unicodeForm()); XERCES_CPP_NAMESPACE::DOMDocument* myDoc; myDoc = impl->createDocument( 0, // root element namespace URI. XStr("response").unicodeForm(), // root element name 0); // document type object (DTD). response->SerializeDom(myDoc); CStdString pingResponse = DomSerializer::DomNodeToString(myDoc); CStdString httpOk("HTTP/1.0 200 OK\r\nContent-type: text/xml\r\n\r\n"); peer().send(httpOk, httpOk.GetLength(), MSG_NOSIGNAL); peer().send(pingResponse, pingResponse.GetLength(), MSG_NOSIGNAL); myDoc->release(); } } else { throw (CStdString("Command not found:") + className); ; } } catch (CStdString &e) { CStdString error("HTTP/1.0 404 not found\r\nContent-type: text/html\r\n\r\nError\r\n"); error = error + e + "\r\n"; peer().send(error, error.GetLength(), MSG_NOSIGNAL); } catch(const XMLException& e) { CStdString error("HTTP/1.0 404 not found\r\nContent-type: text/html\r\n\r\nXML Error\r\n"); peer().send(error, error.GetLength(), MSG_NOSIGNAL); } } else { CStdString notFound("HTTP/1.0 404 not found\r\nContent-type: text/html\r\n\r\nNot found\r\n"); peer().send(notFound, notFound.GetLength(), MSG_NOSIGNAL); } return 0; }
void handleRequest(void* ptr){ struct targs* args = ptr; int sock, copied; time_t seconds; char buf[4096]; char packets[10][4096]; char packet[8192]; int packetIndex; strcpy(packet, ""); sock = args -> newSock; //read in data recv(sock, buf, sizeof(buf), 0); int x; strcat(packet, buf); for(x=0;x<4096;x++)buf[x] = 0; int keepalive = 1; do{ packetIndex = -1; //read whatever is on the socket fully while(copied = recv(sock, buf, sizeof(buf), MSG_DONTWAIT) > 0){ int i; strcat(packet, buf); for(i=0;i<4096;i++)buf[i] = 0; } //extract data from packet if(strlen(packet) > 0){ char packetsCop[8192]; strcpy(packetsCop, packet); //split the received data into packets char* token = strtok(packetsCop, "\n"); while(token != NULL){ int e = strlen(token); if(strstr(token, "HTTP/") != NULL){ packetIndex += 1; strcpy( packets[packetIndex], ""); } strcat(packets[packetIndex], token); strcat(packets[packetIndex], "\n"); token = strtok(NULL, "\n"); } int t; //iterate throught each packet and process appropriately for(t = 0; t <= packetIndex; t++){ //update our timeout seconds = time(NULL); char method[2048]; char http[512]; //check if the method is ok if(methodOk(packets[t], method) < 0){ sendBad(sock, method, 0); //send 400: invalid method keepalive = 0; } //check if the http version is ok else if(httpOk(packets[t], http) < 0){ sendBad(sock, http, 2); //send 400: invalid http version keepalive = 0; } else{ //check whether or not we want to keep the connection open getKeepAlive(packets[t], (int*)&keepalive); char fileName[4096]; int status; //get the file name from the packet and do additional processing status = getFileName(packets[t], fileName); //determine what to do with packet if(status == 200){ sendFile(sock, fileName, keepalive); } else if(status == 404){ sendNotFound(sock, fileName); keepalive = 0; } else if(status == 400){ sendBad(sock, fileName, 1); keepalive = 0; } else if(status == 501){ sendBad(sock, fileName, 3); keepalive = 0; } } } int r; for(r=0;r<packetIndex;r++)packets[r][0] = 0; } int i; for(i=0;i<sizeof(packet);i++)packet[i] = 0; strcpy(packet, ""); }while(keepalive && ((time(NULL)) - (seconds) < 10)); close(sock); free(args); pthread_exit((void*)(0)); }