示例#1
0
void HttpHandler::HandleRequest(const HttpRequest* req, HttpResponse* resp) {
    switch (req->Method()) {
    case HttpRequest::METHOD_HEAD:
        HandleHead(req, resp);
        break;
    case HttpRequest::METHOD_GET:
        HandleGet(req, resp);
        break;
    case HttpRequest::METHOD_POST:
        HandlePost(req, resp);
        break;
    case HttpRequest::METHOD_PUT:
        HandlePut(req, resp);
        break;
    case HttpRequest::METHOD_DELETE:
        HandleDelete(req, resp);
        break;
    case HttpRequest::METHOD_OPTIONS:
        HandleOptions(req, resp);
        break;
    case HttpRequest::METHOD_TRACE:
        HandleTrace(req, resp);
        break;
    case HttpRequest::METHOD_CONNECT:
        HandleConnect(req, resp);
        break;
    default:
        MethodNotAllowed(req, resp);
        break;
    }
}
示例#2
0
//=============================================================================
// Parsing Request
//=============================================================================
//-----------------------------------------------------------------------------
// Main Request Parsing
// RFC2616
//	generic-message = start-line
//		*(message-header CRLF)
//		CRLF
//		[ message-body ]
//	start-line      = Request-Line | Status-Line
//-----------------------------------------------------------------------------
bool CWebserverRequest::HandleRequest(void) {
	std::string start_line = "";
	// read first line
	do {
		start_line = Connection->sock->ReceiveLine();
		if (!Connection->sock->isValid)
			return false;
		if (start_line == "") // Socket empty
		{
			log_level_printf(1, "HandleRequest: End of line not found\n");
			Connection->Response.SendError(HTTP_INTERNAL_SERVER_ERROR);
			Connection->RequestCanceled = true;
			return false;
		}
	} while (start_line == "\r\n"); // ignore empty lines at begin on start-line

	start_line = trim(start_line);
	log_level_printf(1, "Request: %s\n", start_line.c_str());
	UrlData["startline"] = start_line;
	if (!ParseStartLine(start_line))
		return false;

	if (Connection->Method == M_GET || Connection->Method == M_HEAD) {
		std::string tmp_line;
		//read header (speed up: read rest of request in blockmode)
		tmp_line = Connection->sock->ReceiveBlock();
		if (!Connection->sock->isValid) {
			Connection->Response.SendError(HTTP_INTERNAL_SERVER_ERROR);
			return false;
		}

		if (tmp_line == "") {
			Connection->Response.SendError(HTTP_INTERNAL_SERVER_ERROR);
			return false;
		}
		ParseHeader(tmp_line);
	}
	// Other Methods
	if (Connection->Method == M_DELETE || Connection->Method == M_PUT
			|| Connection->Method == M_TRACE) {
		//todo: implement
		aprintf("HTTP Method not implemented :%d\n", Connection->Method);
		Connection->Response.SendError(HTTP_NOT_IMPLEMENTED);
		return false;
	}
	// handle POST (read header & body)
	if (Connection->Method == M_POST) {
		Connection->Response.Write("HTTP/1.1 100 Continue\r\n\r\n"); // POST Requests requires CONTINUE in HTTP/1.1
		return HandlePost();
	}
	// if you are here, something went wrong

	return true;
}