void HTTPRequestHandler::handleRequest() { int err = 0; switch (msg.request) { case HTTP_RT_GET: INFO("Dispatching GET Request."); err = handleGetRequest(); break; case HTTP_RT_POST: INFO("Dispatching POST request."); err = handlePostRequest(); break; case HTTP_RT_PUT: INFO("Dispatching PUT request."); err = handlePutRequest(); break; default: INFO("Error in handleRequest, unhandled request type."); err = HTTP_NotImplemented; break; } // if any of these functions returns a negative number, call the error handler if (err > 0) { handleError(err); } }
uint16_t handleRequest(char* req, cSocket* sock) { if(req == NULL || sock == NULL) return -1; if(strlen(req) == 0) return -1; // init buffer for requests char buffer[1024]; bzero(buffer,1024); uint16_t offset = 0; uint16_t bufferoffset = 0; while(offset < strlen(req) && req[offset] != '\0') { // copy request to buffer buffer[bufferoffset] = req[offset]; // if string is ended if(req[offset] == '\n' || req[offset] == '\0') { buffer[bufferoffset] = '\0'; // check what request is it and create header for request if(preg_match(buffer,"^GET[ ](.+)$") == 0) { uint16_t retcode = handleGetRequest(buffer,sock); if(retcode != HTTP_OK) forgeHeader(retcode,sock,""); return retcode; } else if(preg_match(buffer,"^POST[ ](.+)$") == 0) { uint16_t retcode = handlePostRequest(buffer,sock); if(retcode != HTTP_OK) forgeHeader(retcode,sock,""); return retcode; } else if(preg_match(buffer,"^PUT[ ](.+)$") == 0) { uint16_t retcode = handlePutRequest(buffer,sock); if(retcode != HTTP_OK) forgeHeader(retcode,sock,""); return retcode; } else { forgeHeader(HTTP_ERROR_NOT_IMPLEMENTED,sock,""); return HTTP_ERROR_NOT_IMPLEMENTED; } bzero(buffer,1024); bufferoffset = 0; } else bufferoffset++; offset++; } return 0; }
void receieveMessages(int socket){ char *mesg; mesg = malloc(1); while(1){ if (recv(socket, mesg, 1, 0) >= 1){ if(strcmp(mesg, "0") == 0){ handlePutRequest(socket); } else if(strcmp(mesg, "1") == 0){ handleGetRequest(socket); } else if(strcmp(mesg, "2") == 0){ handleListRequest(socket); } } } }
int handleAssetUpload( ehttp &obj, void * cookie ) { // Variables for Hidden Fields - required to find proper contentitem DWORD dwContentId, dwTitleId, dwContentType; string szSessionId; // Pointers to hold image information as data is uploaded HTTP_ENTRY_DATA assetIcon, assetBanner, assetBoxart, assetBackground; bool hasIconData = false, hasBannerData = false, hasBoxartData = false, hasBackgroundData = false; string szGameTitle, szGameDescription, szGameGenre, szGameDeveloper, szGameExecutable; // Loop through the upload and find the data chunk containing our content id DWORD chunkCount = obj.post_chunks.size(); for(DWORD nChunks = 0; nChunks < chunkCount; nChunks++) { DWORD entryCount = obj.post_chunks.at(nChunks).Entries.size(); for( DWORD nEntries = 0; nEntries < entryCount; nEntries++) { // loop through every entry and find the ones we want if( obj.post_chunks.at(nChunks).Entries.at(nEntries).EntryType == HTTP_ENTRYTYPE_HEADER) { if(obj.post_chunks.at(nChunks).Entries.at(nEntries).Parameters.find("name") != obj.post_chunks.at(nChunks).Entries.at(nEntries).Parameters.end()) { if(obj.post_chunks.at(nChunks).Entries.at(nEntries).Parameters["name"] == "sessionid") { // Grab the Content ID szSessionId = getEntryContent(obj, nChunks); } if(obj.post_chunks.at(nChunks).Entries.at(nEntries).Parameters["name"] == "contentid") { // Grab the Content ID string szContentId = getEntryContent(obj, nChunks); dwContentId = strtoul(szContentId.c_str(), NULL, 16); } if(obj.post_chunks.at(nChunks).Entries.at(nEntries).Parameters["name"] == "titleid") { // Grab the Title ID string szTitleId = getEntryContent(obj, nChunks); dwTitleId = strtoul(szTitleId.c_str(), NULL, 16); } if(obj.post_chunks.at(nChunks).Entries.at(nEntries).Parameters["name"] == "contenttype") { // Grab the ContentType string szContentType = getEntryContent(obj, nChunks); dwContentType = strtoul(szContentType.c_str(), NULL, 16); } if(obj.post_chunks.at(nChunks).Entries.at(nEntries).Parameters["name"] == "GameTitle") { // Grab the Title szGameTitle = getEntryContent(obj, nChunks); } if(obj.post_chunks.at(nChunks).Entries.at(nEntries).Parameters["name"] == "GameDescription") { // Grab the Description szGameDescription = getEntryContent(obj, nChunks); } if(obj.post_chunks.at(nChunks).Entries.at(nEntries).Parameters["name"] == "GameGenre") { // Grab the Genre szGameGenre = getEntryContent(obj, nChunks); } if(obj.post_chunks.at(nChunks).Entries.at(nEntries).Parameters["name"] == "GameDeveloper") { // Grab the Developer szGameDeveloper = getEntryContent(obj, nChunks); } if(obj.post_chunks.at(nChunks).Entries.at(nEntries).Parameters["name"] == "GameExecutable") { // Grab the Executable szGameExecutable = getEntryContent(obj, nChunks); } if(obj.post_chunks.at(nChunks).Entries.at(nEntries).Parameters["name"] == "GameIcon") { if(strcmp(obj.post_chunks.at(nChunks).Entries.at(nEntries).Parameters["filename"].c_str(), "") != 0) { // Extract image data assetIcon = obj.post_chunks.at(nChunks).FileData; hasIconData = true; } } if(obj.post_chunks.at(nChunks).Entries.at(nEntries).Parameters["name"] == "GameBanner") { // check to make sure that a file was uploaded if(strcmp(obj.post_chunks.at(nChunks).Entries.at(nEntries).Parameters["filename"].c_str(), "") != 0) { // Extract image data assetBanner = obj.post_chunks.at(nChunks).FileData; hasBannerData = true; } } if(obj.post_chunks.at(nChunks).Entries.at(nEntries).Parameters["name"] == "GameBoxart") { // check to make sure that a file was uploaded if(strcmp(obj.post_chunks.at(nChunks).Entries.at(nEntries).Parameters["filename"].c_str(), "") != 0) { // Extract image data assetBoxart = obj.post_chunks.at(nChunks).FileData; hasBoxartData = true; } } if(obj.post_chunks.at(nChunks).Entries.at(nEntries).Parameters["name"] == "GameBackground") { // check to make sure that a file was uploaded if(strcmp(obj.post_chunks.at(nChunks).Entries.at(nEntries).Parameters["filename"].c_str(), "") != 0) { // Extract image data assetBackground = obj.post_chunks.at(nChunks).FileData; hasBackgroundData = true; } } } } } } // Verify that the session Id matches before applying changes. if(strcmp(szSessionId.c_str(), HTTPServer::getInstance().g_mGlobalSessionId.c_str()) != 0) return 0; // We have officially received all of our posted data- now let's update our database and send out our messages to the ContentObservers ContentItemNew * pContentItem = NULL; pContentItem = ContentManager::getInstance().GetContentByContentId(dwContentId); if(pContentItem != NULL) { if(pContentItem->getTitleId() != dwTitleId) DebugMsg("HTTPServer", "WARNING: Extracted ContentItem TitleID does not match uploaded content"); // First we'll update our our text items pContentItem->setDisplayInfo(szGameTitle, szGameDescription, szGameGenre, szGameDeveloper); // Now we can start on the images if(hasIconData == true) { DebugMsg("HTTPServer", "GameIcon Path Found, Updating Content."); pContentItem->AddAsset(ITEM_UPDATE_TYPE_THUMBNAIL, assetIcon.Data, assetIcon.Length); } if(hasBannerData == true) { DebugMsg("HTTPServer", "GameBanner Path Found, Updating Content."); pContentItem->AddAsset(ITEM_UPDATE_TYPE_BANNER, assetBanner.Data, assetBanner.Length); } if(hasBoxartData == true) { DebugMsg("HTTPServer", "GameBoxart Path Found, Updating Content."); pContentItem->AddAsset(ITEM_UPDATE_TYPE_BOXART, assetBoxart.Data, assetBoxart.Length); } if(hasBackgroundData == true) { DebugMsg("HTTPServer", "GameBackground Path Found, Updating Content."); pContentItem->AddAsset(ITEM_UPDATE_TYPE_BACKGROUND, assetBackground.Data, assetBackground.Length); } } // Standard link was clicked- let's submit our session data and load the next page HTTP_SESSION_DATA * pData = new HTTP_SESSION_DATA; pData->Username = ""; pData->Password = ""; pData->SessionId = szSessionId; int ret = handleGetRequest( obj, (void*)pData ); return ret; }
int handleScreenshotUpload( ehttp &obj, void * cookie ) { // Variables for Hidden Fields - required to find proper contentitem DWORD dwContentId, dwTitleId, dwContentType, dwScreenshotCount; DWORD dwScreenshotId; string szSessionId; DWORD dwActionType; // 0 = unknown, 1 = replace, 2 = add // Pointers to hold image information as data is uploaded HTTP_ENTRY_DATA assetScreenshot; bool hasScreenshot = false, hasSessionId = false; // Loop through the upload and find the data chunk containing our content id DWORD chunkCount = obj.post_chunks.size(); for(DWORD nChunks = 0; nChunks < chunkCount; nChunks++) { DWORD entryCount = obj.post_chunks.at(nChunks).Entries.size(); for( DWORD nEntries = 0; nEntries < entryCount; nEntries++) { // loop through every entry and find the ones we want if( obj.post_chunks.at(nChunks).Entries.at(nEntries).EntryType == HTTP_ENTRYTYPE_HEADER) { if(obj.post_chunks.at(nChunks).Entries.at(nEntries).Parameters.find("name") != obj.post_chunks.at(nChunks).Entries.at(nEntries).Parameters.end()) { if(obj.post_chunks.at(nChunks).Entries.at(nEntries).Parameters["name"] == "sessionid") { // Grab the Content ID szSessionId = getEntryContent(obj, nChunks); hasSessionId = true; } if(obj.post_chunks.at(nChunks).Entries.at(nEntries).Parameters["name"] == "ssAction") { // Grab the Action Type string szActionType = getEntryContent(obj, nChunks); dwActionType = strtoul(szActionType.c_str(), NULL, 16); } if(obj.post_chunks.at(nChunks).Entries.at(nEntries).Parameters["name"] == "contentid") { // Grab the Content ID string szContentId = getEntryContent(obj, nChunks); dwContentId = strtoul(szContentId.c_str(), NULL, 16); } if(obj.post_chunks.at(nChunks).Entries.at(nEntries).Parameters["name"] == "titleid") { // Grab the Title ID string szTitleId = getEntryContent(obj, nChunks); dwTitleId = strtoul(szTitleId.c_str(), NULL, 16); } if(obj.post_chunks.at(nChunks).Entries.at(nEntries).Parameters["name"] == "contenttype") { // Grab the ContentType string szContentType = getEntryContent(obj, nChunks); dwContentType = strtoul(szContentType.c_str(), NULL, 16); } if(obj.post_chunks.at(nChunks).Entries.at(nEntries).Parameters["name"] == "sscount") { // Grab the Screenshot Count string szScreenshotCount = getEntryContent(obj, nChunks); dwScreenshotCount = strtoul(szScreenshotCount.c_str(), NULL, 16); } if(obj.post_chunks.at(nChunks).Entries.at(nEntries).Parameters["name"] == "SSID") { // Grab the Current Screenshot Id string szScreenshotId= getEntryContent(obj, nChunks); dwScreenshotId = atoi(szScreenshotId.c_str()); if(dwScreenshotId < 0) dwScreenshotId = 0; if(dwScreenshotId > (DWORD)SETTINGS::getInstance().getMaxScreenshots()) dwScreenshotId = SETTINGS::getInstance().getMaxScreenshots(); } if(obj.post_chunks.at(nChunks).Entries.at(nEntries).Parameters["name"] == "GameScreenshot") { // check to make sure that a file was uploaded if(strcmp(obj.post_chunks.at(nChunks).Entries.at(nEntries).Parameters["filename"].c_str(), "") != 0) { // Extract image data assetScreenshot = obj.post_chunks.at(nChunks).FileData; hasScreenshot = true; } } } } } } // Verify that the session Id matches before applying changes. if(strcmp(szSessionId.c_str(), HTTPServer::getInstance().g_mGlobalSessionId.c_str()) != 0) return 0; // We have officially received all of our posted data- now let's update our database and send out our messages to the ContentObservers ContentItemNew * pContentItem = NULL; pContentItem = ContentManager::getInstance().GetContentByContentId(dwContentId); if(pContentItem != NULL) { // Handle uploaded content to the database at this point CONTENT_SCREENSHOT_INFO ssInfo; switch (dwActionType) { case 0: // Replace Action ssInfo.nScreenshotIndex = dwScreenshotId; ssInfo.nScreenshotFlag = CONTENT_SCREENSHOT_FLAG_REPLACE; pContentItem->AddAsset(ITEM_UPDATE_TYPE_SCREENSHOT, (VOID*) assetScreenshot.Data, assetScreenshot.Length, (VOID*)&ssInfo); break; case 1: // Add Action if(dwScreenshotCount == SETTINGS::getInstance().getMaxScreenshots()) { // Replace last screenshot if count is max ssInfo.nScreenshotFlag = CONTENT_SCREENSHOT_FLAG_REPLACE; ssInfo.nScreenshotIndex = SETTINGS::getInstance().getMaxScreenshots() - 1; } else { // Add additional screenshots ssInfo.nScreenshotFlag = CONTENT_SCREENSHOT_FLAG_ADD; ssInfo.nScreenshotIndex = dwScreenshotCount; } pContentItem->AddScreenshot(); pContentItem->AddAsset(ITEM_UPDATE_TYPE_SCREENSHOT, (VOID*) assetScreenshot.Data, assetScreenshot.Length, (VOID*)&ssInfo); break; default: break; }; } // Standard link was clicked- let's submit our session data and load the next page HTTP_SESSION_DATA * pData = new HTTP_SESSION_DATA; pData->Username = ""; pData->Password = ""; pData->SessionId = szSessionId; int ret = handleGetRequest( obj, (void*)pData ); return ret; }