Exemplo n.º 1
0
/**
 * Parse
 * Populate internal HTTPRequest variables by parsing the HTTP data
 *
 * @param True if successful. If false, sets parseErrorStr for reason of failure
 */
bool HTTPRequest::parse() {
	std::string initial = "", methodName = "";

	// Get elements from the initial line: <method> <path> <version>\r\n
	methodName = getStrElement();
	requestUri = getStrElement();
	version = getLine(); // End of the line, pull till \r\n

	// Convert the name to the internal enumeration number
	method = methodStrToInt(methodName);
	if(method == -1) {
		parseErrorStr = "Invalid Method: " + methodName;
		return false;
	}

	// Validate the HTTP version. If there is a mismatch, discontinue parsing
	if(strcmp(version.c_str(), HTTP_VERSION) != 0) {
		parseErrorStr = "Supported HTTP version does not match";
		return false;
	}

	// Parse and populate the headers map using the parseHeaders helper
	parseHeaders();

	// Only POST and PUT can have Content (data after headers)
	if((method != POST) && (method != PUT))
		return true;
	
	// Parse the body of the message
	if(!parseBody())
		return false;
	
	return true;
}
Exemplo n.º 2
0
bool HttpResponse::parse(){
    std::string statusstr;

    version = getStrElement();
    statusstr = getStrElement();
    determineStatusCode();
    reason = getLine();

    parseHeaders();

    if(!parseBody())
   	return false;

    return true;
}