Rewriting url parser

This commit is contained in:
Meeh 2014-03-29 18:28:19 +01:00
parent 02a42b0e98
commit 5fa7671ef7
2 changed files with 28 additions and 28 deletions

View File

@ -1,6 +1,7 @@
#include <boost/bind.hpp> #include <boost/bind.hpp>
#include <boost/lexical_cast.hpp> #include <boost/lexical_cast.hpp>
#include <boost/bind/protect.hpp> #include <boost/bind/protect.hpp>
#include <boost/regex.hpp>
#include "base64.h" #include "base64.h"
#include "Log.h" #include "Log.h"
@ -62,12 +63,12 @@ namespace proxy
{ {
m_Buffer[bytes_transferred] = 0; m_Buffer[bytes_transferred] = 0;
std::pair<std::string,std::string> requestInfo = ExtractRequest ();
request m_Request; request m_Request;
ExtractRequest (m_Request);
parseHeaders (m_Buffer, m_Request.headers); parseHeaders (m_Buffer, m_Request.headers);
LogPrint("Requesting ", requestInfo.first, " with path ", requestInfo.second); LogPrint("Requesting ", m_Request.host, " with path ", m_Request.uri, " and method ", m_Request.method);
HandleDestinationRequest (requestInfo.first, requestInfo.second); HandleDestinationRequest (m_Request.host, m_Request.uri);
} }
else if (ecode != boost::asio::error::operation_aborted) else if (ecode != boost::asio::error::operation_aborted)
Terminate (); Terminate ();
@ -96,33 +97,31 @@ namespace proxy
} }
} }
// TODO: Support other requests than GET. void HTTPConnection::ExtractRequest (request &m_Request)
std::pair<std::string, std::string> HTTPConnection::ExtractRequest ()
{ {
char * get = strstr (m_Buffer, "GET"); std::string requestString = m_Buffer;
if (get) int idx=requestString.find(" ");
{ std::string method = requestString.substr(0,idx);
char * http = strstr (get, "HTTP"); requestString = requestString.substr(idx+1);
if (http) idx=requestString.find(" ");
{ std::string requestUrl = requestString.substr(0,idx);
std::string url (get + 4, http - get - 5); LogPrint("method is: ", method, "\nRequest is: ", requestUrl);
size_t sp = url.find_first_of ('/', 7 /* skip http:// part */ ); std::string server="";
if (sp != std::string::npos) std::string port="80";
{ boost::regex rHTTP("http://(.*?)(:(\\d+))?(/.*)");
std::string base_url (url.begin()+7, url.begin()+sp); boost::smatch m;
LogPrint ("Base URL is: ", base_url, "\n"); std::string path;
if ( sp != std::string::npos ) if(boost::regex_search(requestUrl, m, rHTTP, boost::match_extra)) {
{ server=m[1].str();
std::string query (url.begin ()+sp+1, url.end ()); if(m[2].str() != "") {
LogPrint ("Query is: ", "/" + query); port=m[3].str();
return std::make_pair (base_url, "/" + query);
}
return std::make_pair (base_url, "/");
}
} }
path=m[4].str();
} }
return std::make_pair ("",""); LogPrint("server is: ",server, "\n path is: ",path);
m_Request.uri = path;
m_Request.method = method;
m_Request.host = server;
} }
void HTTPConnection::HandleWriteReply (const boost::system::error_code& ecode) void HTTPConnection::HandleWriteReply (const boost::system::error_code& ecode)

View File

@ -24,6 +24,7 @@ namespace proxy
{ {
std::string method; std::string method;
std::string uri; std::string uri;
std::string host;
int http_version_major; int http_version_major;
int http_version_minor; int http_version_minor;
std::vector<header> headers; std::vector<header> headers;
@ -54,7 +55,7 @@ namespace proxy
void SendReply (const std::string& content); void SendReply (const std::string& content);
void HandleDestinationRequest (const std::string& address, const std::string& uri); void HandleDestinationRequest (const std::string& address, const std::string& uri);
std::pair<std::string, std::string> ExtractRequest (); void ExtractRequest (request& m_Request);
void parseHeaders(const std::string& h, std::vector<header>& hm); void parseHeaders(const std::string& h, std::vector<header>& hm);
private: private: