示例#1
0
	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;
	}
示例#2
0
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;
}