static int requestHandler(void* cls, MHD_Connection* conn, const char* url, const char* method, const char* version, const char* upload_data, size_t* upload_data_size, void** con_cls) { printf("requestHandler %s\n", url); // find handler RequestHandler* handler = NULL; const char* urlPart = NULL; PatternMap::const_iterator itr = sRootPatterns.begin(); while(itr != sRootPatterns.end()) { const string& pattern(itr->first); if(strncmp(url, pattern.c_str(), pattern.size()) == 0) { handler = itr->second; urlPart = url + pattern.size(); break; } ++itr; } if(handler == NULL) { itr = sExactPatterns.find(url); if(itr != sExactPatterns.end()) { handler = itr->second; urlPart = url + itr->first.size(); } } if(handler == NULL) { // 404 static char s404[] = "Request handler not found."; MHD_Response* resp = MHD_create_response_from_data(sizeof(s404)-1, s404, 0, 0); int res = MHD_queue_response(conn, 404, resp); assert(res == MHD_YES); MHD_destroy_response(resp); *con_cls = NULL; return MHD_YES; } ResponseData* rd = handler->handleRequest(urlPart, conn); if(rd) rd->handler = handler; *con_cls = rd; return MHD_YES; }
ResponseData* handleRequest(const char* urlPart, MHD_Connection* conn) { int res; MHD_Response* resp; // parse & unload string responseText = "Unloaded:"; const char* ptr = urlPart; while(*ptr) { // find next unload request const char* end = strchr(ptr, ','); size_t len = end ? end - ptr : strlen(ptr); const char* colon = (char*)memchr(ptr, ':', len); const char* newName = NULL; size_t totalLen = len; if(colon) { // new name for dll newName = colon + 1; len = colon - ptr; } // search root patterns for(PatternMap::const_iterator itr = sRootPatterns.begin(); itr != sRootPatterns.end(); ++itr) { const string& pattern(itr->first); size_t min = MIN(len, pattern.size() - 2); // if request was found in patterns if(strncmp(ptr, pattern.c_str() + 1, min) == 0) { // prepare to report it responseText += ' '; responseText.append(ptr, len); // perform the unload printf("Unloading %.*s...\n", (int)len, ptr); itr->second->unload(newName, totalLen - (len+1)); } } ptr += totalLen; if(*ptr == ',') ptr++; } printf("Unload complete.\n"); // send the response resp = MHD_create_response_from_data(responseText.size(), (void*)responseText.c_str(), 0, 1); MHD_add_response_header(resp, MHD_HTTP_HEADER_CONTENT_TYPE, "text/plain"); res = MHD_queue_response(conn, 200, resp); assert(res == MHD_YES); MHD_destroy_response(resp); return NULL; }
bool readOldElementsFile( const char *filename, ElementMap &table, const PatternMap &patterns) { FILE *tableFile = fopen(filename, "rb"); try { if (tableFile != NULL) { for (;;) { unsigned char numBuf[4]; unsigned int ldrawNum; int flags; char ldrawBase[128]; std::string ldrawFilename; Element element; size_t readSize = fread(numBuf, 1, 4, tableFile); PatternMap::const_iterator it1; if (readSize == 0) { // done break; } else if (readSize != 4) { throw "Error parsing %s.\n"; } ldrawNum = ((unsigned int)numBuf[0] << 24) | ((unsigned int)numBuf[1] << 16) | ((unsigned int)numBuf[2] << 8) | (unsigned int)numBuf[3]; flags = fgetc(tableFile); if (flags == EOF) { throw "Error parsing %s.\n"; } element.flags = (unsigned int)flags; element.lgeoName.reserve(10); if (!readString(tableFile, element.lgeoName)) { throw "Error parsing %s.\n"; } element.lgeoFilename = element.lgeoName + ".inc"; sprintf(ldrawBase, "%d", ldrawNum); ldrawFilename = ldrawBase; ldrawFilename += ".dat"; table[ldrawFilename] = element; it1 = patterns.find(element.lgeoName.substr(3)); if (it1 != patterns.end()) { const StringStringMap &strings = it1->second; for (StringStringMap::const_iterator it2 = strings.begin(); it2 != strings.end(); it2++) { element.lgeoFilename = "patterns/"; element.lgeoFilename += it2->second; element.lgeoFilename += ".inc"; element.lgeoName = "lg_"; element.lgeoName += it1->first; element.lgeoName += 'p'; element.lgeoName += it2->first; ldrawFilename = ldrawBase; ldrawFilename += 'p'; ldrawFilename += it2->first; ldrawFilename += ".dat"; table[ldrawFilename] = element; } } } fclose(tableFile); return true; } else { throw "Error reading %s.\n"; } } catch (const char *error) { printf(error, filename); if (tableFile != NULL) { fclose(tableFile); } return false; } }