Ejemplo n.º 1
0
msl::http_response msl::post_request(const std::string& host,const std::string& request,const std::string& data)
{
	msl::http_response ret{"",{},""};
	msl::tcp_socket_t client("0.0.0.0:0>"+host);
	client.open();

	if(!client.good())
		return ret;

	std::string request_header="POST "+request+" HTTP/1.1\r\n";
	request_header+="Connection: close\r\n";
	request_header+="Content-Length: "+std::to_string(data.size())+"\r\n";
	request_header+="Content-Type: text/plain\r\n";
	request_header+="\r\n";
	request_header+=data;
	client.write(request_header);

	std::string response="";
	uint8_t temp;

	while(client.available()>=0&&client.read(&temp,1)==1)
		response+=temp;

	client.close();

	return parse_http_response(response);
}
Ejemplo n.º 2
0
msl::http_response msl::get_request(const std::string& host,const std::string& request)
{
	msl::http_response ret{"",{},""};
	msl::tcp_socket_t client("0.0.0.0:0>"+host);
	client.open();

	if(!client.good())
		return ret;

	std::string request_header="GET "+request+" HTTP/1.1\r\n";
	request_header+="Connection: close\r\n";
	request_header+="\r\n";
	client.write(request_header);

	std::string response="";
	uint8_t temp;

	while(client.available()>=0&&client.read(&temp,1)==1)
		response+=temp;

	client.close();

	return parse_http_response(response);
}
Ejemplo n.º 3
0
bool CHttpClient::ParseHeaders(String& strBuffer, int& iBufferSize)
{
	// Find the header size, testing code, but should work
	mg_request_info info;
	
	char* buf = new char[iBufferSize];
	memcpy(buf, strBuffer.C_String(), iBufferSize);
	parse_http_response(buf, strBuffer.GetLength(), &info);
	
	String buf_header;
	int iHeaderSize = 0;
	for(int i = 0; i < info.num_headers; ++i)
	{
		buf_header.AppendF("%s: %s\r\n", info.http_headers[i].name, info.http_headers[i].value);
	}

	iHeaderSize = buf_header.GetLength();
	iHeaderSize += (info.request_method != NULL ? strlen(info.request_method) : 0) + (info.uri != NULL ? strlen(info.uri) : 0) + (info.http_version != NULL ? strlen(info.http_version) : 0) + strlen("\r\n\r\n\r\n");
	iBufferSize -= iHeaderSize;
	strBuffer.Erase(0, iHeaderSize);
	m_headerMap["HeaderSize"] = iHeaderSize;


	// ADAMIX/JENKSTA: commented out this code because doesn't work properly. Are we really need to parse headers?

	/*// Ignore all initial whitespace
	unsigned int uiWhiteSpace = 0;

	for(unsigned int i = 0; i < (unsigned int)iBufferSize; i++)
	{
		// Is this whitespace?
		if(strBuffer[i] == ' ')
		{
			// Increment the whitespace amount
			uiWhiteSpace++;
			continue;
		}

		// Finished
		break;
	}

	// Remove whitespace
	if(uiWhiteSpace > 0)
		strBuffer.Erase(0, uiWhiteSpace);

	// Ignore the version, status code and status message
	// TODO: Use this information?
	// Will be in format 'HTTP/1.0 200 OK\r\n'
	unsigned int uiIgnore = strBuffer.Find("\r\n");

	if(uiIgnore == String::nPos)
		return false;

	strBuffer.Erase(0, (uiIgnore + 2));
	iBufferSize -= (uiIgnore + 2);

	// Find all headers
	unsigned int uiContentLength = String::nPos;
	unsigned int uiNameSplit;
	unsigned int uiValueSplit;

	while((uiNameSplit = strBuffer.Find(": ")) != String::nPos)
	{
		// Do we have a content length?
		if(uiContentLength != String::nPos)
		{
			// Get the content start
			unsigned int uiContentStart = (iBufferSize - uiContentLength);

			// Is the find over the content start?
			if(uiNameSplit >= uiContentStart)
				break;
		}

		// Find the value end
		uiValueSplit = strBuffer.Find("\r\n");

		// Did we not find a value end?
		if(uiValueSplit == String::nPos)
			return false;

		// Get the header name
		String strName = strBuffer.SubStr(0, uiNameSplit);

		// If this is an accept-ranges header get the value from the next header
		// jenksta: not sure if this is right, but this is how it works with 'accept-ranges: bytes'
		// in mongoose
		if(!strName.ICompare("accept-ranges"))
		{
			// Find the value end
			uiValueSplit = strBuffer.Find("\r\n", (uiValueSplit + 2));

			// Did we not find a value end?
			if(uiValueSplit == String::nPos)
				return false;
		}

		// Get the header value
		String strValue = strBuffer.SubStr((uiNameSplit + 2), (uiValueSplit - (uiNameSplit + 2)));

		// Add the header to the header map
		m_headerMap[strName] = strValue;

		// Erase the header from the buffer
		strBuffer.Erase(0, (uiValueSplit + 2));
		iBufferSize -= (uiValueSplit + 2);

		// Is this the content length header?
		if(!strName.ICompare("content-length"))
		{
			// Set the content length
			uiContentLength = strValue.ToInteger();
		}
	}

	// Did we not get any headers?
	if(m_headerMap.empty())
		return false;
	*/
	// Success
	return true;
}