HTTP parser updates to Digest auth and support for getProxyUrl() for proxying, SendRequest with payload by reference.
This commit is contained in:
parent
a36cbfa1c5
commit
ce9aae3095
2 changed files with 410 additions and 378 deletions
|
@ -2,10 +2,11 @@
|
|||
/// Holds all code for the HTTP namespace.
|
||||
|
||||
#include "http_parser.h"
|
||||
#include "auth.h"
|
||||
#include "defines.h"
|
||||
#include "encode.h"
|
||||
#include "timing.h"
|
||||
#include "defines.h"
|
||||
#include "auth.h"
|
||||
#include <iomanip>
|
||||
|
||||
/// Helper function to check if the given c-string is numeric or not
|
||||
static bool is_numeric(const char *str){
|
||||
|
@ -26,9 +27,7 @@ HTTP::URL::URL(const std::string & url){
|
|||
proto_sep += 3;
|
||||
}else{
|
||||
proto_sep = 0;
|
||||
if (url.substr(0, 2) == "//"){
|
||||
proto_sep = 2;
|
||||
}
|
||||
if (url.substr(0, 2) == "//"){proto_sep = 2;}
|
||||
}
|
||||
// proto_sep now points to the start of the host, guaranteed
|
||||
// continue by finding the path, if any
|
||||
|
@ -50,9 +49,7 @@ HTTP::URL::URL(const std::string & url){
|
|||
path.erase(qmark);
|
||||
}
|
||||
if (path.size()){
|
||||
if (path[0] == '/'){
|
||||
path.erase(0, 1);
|
||||
}
|
||||
if (path[0] == '/'){path.erase(0, 1);}
|
||||
size_t dots = path.find("/./");
|
||||
while (dots != std::string::npos){
|
||||
DONTEVEN_MSG("%s (/./ -> /)", path.c_str());
|
||||
|
@ -65,9 +62,7 @@ HTTP::URL::URL(const std::string & url){
|
|||
path.erase(dots, 1);
|
||||
dots = path.find("//");
|
||||
}
|
||||
if (path[0] == '/'){
|
||||
path.erase(0, 1);
|
||||
}
|
||||
if (path[0] == '/'){path.erase(0, 1);}
|
||||
dots = path.find("/../");
|
||||
while (dots != std::string::npos){
|
||||
size_t prevslash = path.rfind('/', dots - 1);
|
||||
|
@ -78,12 +73,8 @@ HTTP::URL::URL(const std::string & url){
|
|||
}
|
||||
dots = path.find("/../");
|
||||
}
|
||||
if (path.substr(0, 2) == "./"){
|
||||
path.erase(0, 2);
|
||||
}
|
||||
if (path.substr(0, 3) == "../"){
|
||||
path.erase(0, 3);
|
||||
}
|
||||
if (path.substr(0, 2) == "./"){path.erase(0, 2);}
|
||||
if (path.substr(0, 3) == "../"){path.erase(0, 3);}
|
||||
path = Encodings::URL::decode(path);
|
||||
}
|
||||
}
|
||||
|
@ -191,6 +182,26 @@ std::string HTTP::URL::getUrl() const{
|
|||
return ret;
|
||||
}
|
||||
|
||||
/// Returns the URL in string format without auth and frag
|
||||
std::string HTTP::URL::getProxyUrl() const{
|
||||
std::string ret;
|
||||
if (protocol.size()){
|
||||
ret = protocol + "://";
|
||||
}else{
|
||||
ret = "//";
|
||||
}
|
||||
if (IPv6Addr){
|
||||
ret += "[" + host + "]";
|
||||
}else{
|
||||
ret += host;
|
||||
}
|
||||
if (port.size() && getPort() != getDefaultPort()){ret += ":" + port;}
|
||||
ret += "/";
|
||||
if (path.size()){ret += Encodings::URL::encode(path);}
|
||||
if (args.size()){ret += "?" + args;}
|
||||
return ret;
|
||||
}
|
||||
|
||||
/// Returns the URL in string format without args and frag
|
||||
std::string HTTP::URL::getBareUrl() const{
|
||||
std::string ret;
|
||||
|
@ -252,15 +263,20 @@ HTTP::URL HTTP::URL::link(const std::string &l) const{
|
|||
HTTP::Parser::Parser(){
|
||||
headerOnly = false;
|
||||
Clean();
|
||||
std::stringstream nStr;
|
||||
nStr << std::hex << std::setw(16) << std::setfill('0') << (uint64_t)(Util::bootMS());
|
||||
cnonce = nStr.str();
|
||||
}
|
||||
|
||||
/// Completely re-initializes the HTTP::Parser, leaving it ready for either reading or writing usage.
|
||||
/// Completely re-initializes the HTTP::Parser, leaving it ready for either reading or writing
|
||||
/// usage.
|
||||
void HTTP::Parser::Clean(){
|
||||
CleanPreserveHeaders();
|
||||
headers.clear();
|
||||
}
|
||||
|
||||
/// Completely re-initializes the HTTP::Parser, leaving it ready for either reading or writing usage.
|
||||
/// Completely re-initializes the HTTP::Parser, leaving it ready for either reading or writing
|
||||
/// usage.
|
||||
void HTTP::Parser::CleanPreserveHeaders(){
|
||||
seenHeaders = false;
|
||||
seenReq = false;
|
||||
|
@ -278,17 +294,25 @@ void HTTP::Parser::CleanPreserveHeaders() {
|
|||
/// Local-only helper function for use in auth()
|
||||
/// Returns the string contents of the given val from list
|
||||
static std::string findValIn(const std::string &list, const std::string &val){
|
||||
size_t pos = list.find(val+"=\"");
|
||||
size_t pos = list.find(val + "=");
|
||||
if (pos == std::string::npos){return "";}
|
||||
pos += val.size() + 2;
|
||||
pos += val.size() + 1;
|
||||
if (pos >= list.size()){return "";}
|
||||
size_t ePos = list.find('"', pos);
|
||||
size_t ePos;
|
||||
if (list[pos] == '"'){
|
||||
++pos;
|
||||
ePos = list.find('"', pos);
|
||||
if (ePos == std::string::npos){return "";}
|
||||
}else{
|
||||
ePos = list.find(',', pos);
|
||||
}
|
||||
return list.substr(pos, ePos - pos);
|
||||
}
|
||||
|
||||
/// Attempts to send an authentication header with the given name and password. Uses authReq as WWW-Authenticate header.
|
||||
void HTTP::Parser::auth(const std::string & user, const std::string & pass, const std::string & authReq){
|
||||
/// Attempts to send an authentication header with the given name and password. Uses authReq as
|
||||
/// WWW-Authenticate header.
|
||||
void HTTP::Parser::auth(const std::string &user, const std::string &pass,
|
||||
const std::string &authReq, const std::string &headerName){
|
||||
size_t space = authReq.find(' ');
|
||||
if (space == std::string::npos || !user.size() || !pass.size()){
|
||||
FAIL_MSG("No authentication possible");
|
||||
|
@ -296,15 +320,48 @@ void HTTP::Parser::auth(const std::string & user, const std::string & pass, cons
|
|||
}
|
||||
std::string meth = authReq.substr(0, space);
|
||||
if (meth == "Basic"){
|
||||
SetHeader("Authorization", "Basic "+Encodings::Base64::encode(user+":"+pass));
|
||||
SetHeader(headerName, "Basic " + Encodings::Base64::encode(user + ":" + pass));
|
||||
return;
|
||||
}
|
||||
if (meth == "Digest"){
|
||||
std::string realm=findValIn(authReq, "realm"), nonce=findValIn(authReq, "nonce");
|
||||
std::string realm = findValIn(authReq, "realm"), nonce = findValIn(authReq, "nonce"),
|
||||
opaque = findValIn(authReq, "opaque"), qop = findValIn(authReq, "qop"),
|
||||
algo = findValIn(authReq, "algorithm");
|
||||
std::string A1 = Secure::md5(user + ":" + realm + ":" + pass);
|
||||
std::string A2 = Secure::md5(method+":"+url);
|
||||
std::string response = Secure::md5(A1+":"+nonce+":"+A2);
|
||||
SetHeader("Authorization", "Digest username=\""+user+"\", realm=\""+realm+"\", nonce=\""+nonce+"\", uri=\""+url+"\", response=\""+response+"\"");
|
||||
if (algo.size() && algo != "MD5"){
|
||||
FAIL_MSG("Authorization algorithm %s not implemented", algo.c_str());
|
||||
return;
|
||||
}
|
||||
std::string urlPart;
|
||||
if (url.find("://") != std::string::npos){
|
||||
HTTP::URL tmpUrl(url);
|
||||
urlPart = "/" + tmpUrl.path;
|
||||
}else{
|
||||
urlPart = url;
|
||||
}
|
||||
algo = "MD5";
|
||||
std::string A2 = Secure::md5(method + ":" + urlPart);
|
||||
std::string response;
|
||||
static uint32_t nc = 0;
|
||||
std::string ncStr;
|
||||
if (qop.size()){
|
||||
++nc;
|
||||
std::stringstream nHex;
|
||||
nHex << std::hex << std::setw(8) << std::setfill('0') << nc;
|
||||
ncStr = nHex.str();
|
||||
response = Secure::md5(A1 + ":" + nonce + ":" + ncStr + ":" + cnonce + ":auth:" + A2);
|
||||
}else{
|
||||
response = Secure::md5(A1 + ":" + nonce + ":" + A2);
|
||||
}
|
||||
std::stringstream rep;
|
||||
// username | realm | nonce | digest-uri | response | [ algorithm ] | [cnonce] | [opaque] |
|
||||
// [message-qop] | [nonce-count]
|
||||
rep << "Digest username=\"" << user << "\", realm=\"" << realm << "\", nonce=\"" << nonce
|
||||
<< "\", uri=\"" << urlPart << "\", response=\"" << response << "\", algorithm=" + algo;
|
||||
if (qop.size()){rep << ", cnonce=\"" << cnonce << "\"";}
|
||||
if (opaque.size()){rep << ", opaque=\"" << opaque << "\"";}
|
||||
if (qop.size()){rep << ", qop=auth, nc=" << ncStr;}
|
||||
SetHeader(headerName, rep.str());
|
||||
return;
|
||||
}
|
||||
FAIL_MSG("No authentication possible, unimplemented method '%s'", meth.c_str());
|
||||
|
@ -325,7 +382,6 @@ void HTTP::Parser::setCORSHeaders(){
|
|||
SetHeader("Expires", "0");
|
||||
}
|
||||
|
||||
|
||||
/// Returns a string containing a valid HTTP 1.0 or 1.1 request, ready for sending.
|
||||
/// The request is build from internal variables set before this call is made.
|
||||
/// To be precise, method, url, protocol, headers and body are used.
|
||||
|
@ -333,9 +389,7 @@ void HTTP::Parser::setCORSHeaders(){
|
|||
std::string &HTTP::Parser::BuildRequest(){
|
||||
/// \todo Include GET/POST variable parsing?
|
||||
std::map<std::string, std::string>::iterator it;
|
||||
if (protocol.size() < 5 || protocol[4] != '/') {
|
||||
protocol = "HTTP/1.0";
|
||||
}
|
||||
if (protocol.size() < 5 || protocol[4] != '/'){protocol = "HTTP/1.0";}
|
||||
builder = method + " " + url + " " + protocol + "\r\n";
|
||||
for (it = headers.begin(); it != headers.end(); it++){
|
||||
if ((*it).first != "" && (*it).second != ""){
|
||||
|
@ -349,14 +403,13 @@ std::string & HTTP::Parser::BuildRequest() {
|
|||
/// Creates and sends a valid HTTP 1.0 or 1.1 request.
|
||||
/// The request is build from internal variables set before this call is made.
|
||||
/// To be precise, method, url, protocol, headers and body are used.
|
||||
void HTTP::Parser::SendRequest(Socket::Connection & conn) {
|
||||
void HTTP::Parser::SendRequest(Socket::Connection &conn, const std::string &reqbody){
|
||||
/// \todo Include GET/POST variable parsing?
|
||||
std::map<std::string, std::string>::iterator it;
|
||||
if (protocol.size() < 5 || protocol[4] != '/') {
|
||||
protocol = "HTTP/1.0";
|
||||
}
|
||||
if (protocol.size() < 5 || protocol[4] != '/'){protocol = "HTTP/1.0";}
|
||||
builder = method + " " + url + " " + protocol + "\r\n";
|
||||
conn.SendNow(builder);
|
||||
if (reqbody.size()){SetHeader("Content-Length", reqbody.length());}
|
||||
for (it = headers.begin(); it != headers.end(); it++){
|
||||
if ((*it).first != "" && (*it).second != ""){
|
||||
builder = (*it).first + ": " + (*it).second + "\r\n";
|
||||
|
@ -364,8 +417,12 @@ void HTTP::Parser::SendRequest(Socket::Connection & conn) {
|
|||
}
|
||||
}
|
||||
conn.SendNow("\r\n", 2);
|
||||
if (reqbody.size()){
|
||||
conn.SendNow(reqbody);
|
||||
}else{
|
||||
conn.SendNow(body);
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns a string containing a valid HTTP 1.0 or 1.1 response, ready for sending.
|
||||
/// The response is partly build from internal variables set before this call is made.
|
||||
|
@ -376,9 +433,7 @@ void HTTP::Parser::SendRequest(Socket::Connection & conn) {
|
|||
std::string &HTTP::Parser::BuildResponse(std::string code, std::string message){
|
||||
/// \todo Include GET/POST variable parsing?
|
||||
std::map<std::string, std::string>::iterator it;
|
||||
if (protocol.size() < 5 || protocol[4] != '/') {
|
||||
protocol = "HTTP/1.0";
|
||||
}
|
||||
if (protocol.size() < 5 || protocol[4] != '/'){protocol = "HTTP/1.0";}
|
||||
builder = protocol + " " + code + " " + message + "\r\n";
|
||||
for (it = headers.begin(); it != headers.end(); it++){
|
||||
if ((*it).first != "" && (*it).second != ""){
|
||||
|
@ -404,16 +459,13 @@ std::string & HTTP::Parser::BuildResponse() {
|
|||
/// Creates and sends a valid HTTP 1.0 or 1.1 response.
|
||||
/// The response is partly build from internal variables set before this call is made.
|
||||
/// To be precise, protocol, headers and body are used.
|
||||
/// This call will attempt to buffer as little as possible and block until the whole request is sent.
|
||||
/// \param code The HTTP response code. Usually you want 200.
|
||||
/// \param message The HTTP response message. Usually you want "OK".
|
||||
/// \param conn The Socket::Connection to send the response over.
|
||||
/// This call will attempt to buffer as little as possible and block until the whole request is
|
||||
/// sent. \param code The HTTP response code. Usually you want 200. \param message The HTTP response
|
||||
/// message. Usually you want "OK". \param conn The Socket::Connection to send the response over.
|
||||
void HTTP::Parser::SendResponse(std::string code, std::string message, Socket::Connection &conn){
|
||||
/// \todo Include GET/POST variable parsing?
|
||||
std::map<std::string, std::string>::iterator it;
|
||||
if (protocol.size() < 5 || protocol[4] != '/') {
|
||||
protocol = "HTTP/1.0";
|
||||
}
|
||||
if (protocol.size() < 5 || protocol[4] != '/'){protocol = "HTTP/1.0";}
|
||||
builder = protocol + " " + code + " " + message + "\r\n";
|
||||
conn.SendNow(builder);
|
||||
for (it = headers.begin(); it != headers.end(); it++){
|
||||
|
@ -430,14 +482,15 @@ void HTTP::Parser::SendResponse(std::string code, std::string message, Socket::C
|
|||
|
||||
/// Creates and sends a valid HTTP 1.0 or 1.1 response, based on the given request.
|
||||
/// The headers must be set before this call is made.
|
||||
/// This call sets up chunked transfer encoding if the request was protocol HTTP/1.1, otherwise uses a zero-content-length HTTP/1.0 response.
|
||||
/// \param code The HTTP response code. Usually you want 200.
|
||||
/// \param message The HTTP response message. Usually you want "OK".
|
||||
/// \param request The HTTP request to respond to.
|
||||
/// \param conn The connection to send over.
|
||||
void HTTP::Parser::StartResponse(std::string code, std::string message, HTTP::Parser & request, Socket::Connection & conn, bool bufferAllChunks) {
|
||||
/// This call sets up chunked transfer encoding if the request was protocol HTTP/1.1, otherwise uses
|
||||
/// a zero-content-length HTTP/1.0 response. \param code The HTTP response code. Usually you want
|
||||
/// 200. \param message The HTTP response message. Usually you want "OK". \param request The HTTP
|
||||
/// request to respond to. \param conn The connection to send over.
|
||||
void HTTP::Parser::StartResponse(std::string code, std::string message, HTTP::Parser &request,
|
||||
Socket::Connection &conn, bool bufferAllChunks){
|
||||
std::string prot = request.protocol;
|
||||
sendingChunks = (!bufferAllChunks && protocol == "HTTP/1.1" && request.GetHeader("Connection")!="close");
|
||||
sendingChunks =
|
||||
(!bufferAllChunks && protocol == "HTTP/1.1" && request.GetHeader("Connection") != "close");
|
||||
CleanPreserveHeaders();
|
||||
protocol = prot;
|
||||
if (sendingChunks){
|
||||
|
@ -446,22 +499,22 @@ void HTTP::Parser::StartResponse(std::string code, std::string message, HTTP::Pa
|
|||
SetHeader("Connection", "close");
|
||||
}
|
||||
bufferChunks = bufferAllChunks;
|
||||
if (!bufferAllChunks){
|
||||
SendResponse(code, message, conn);
|
||||
}
|
||||
if (!bufferAllChunks){SendResponse(code, message, conn);}
|
||||
}
|
||||
|
||||
/// Creates and sends a valid HTTP 1.0 or 1.1 response, based on the given request.
|
||||
/// The headers must be set before this call is made.
|
||||
/// This call sets up chunked transfer encoding if the request was protocol HTTP/1.1, otherwise uses a zero-content-length HTTP/1.0 response.
|
||||
/// This call simply calls StartResponse("200", "OK", request, conn)
|
||||
/// \param request The HTTP request to respond to.
|
||||
/// \param conn The connection to send over.
|
||||
void HTTP::Parser::StartResponse(HTTP::Parser & request, Socket::Connection & conn, bool bufferAllChunks) {
|
||||
/// This call sets up chunked transfer encoding if the request was protocol HTTP/1.1, otherwise uses
|
||||
/// a zero-content-length HTTP/1.0 response. This call simply calls StartResponse("200", "OK",
|
||||
/// request, conn) \param request The HTTP request to respond to. \param conn The connection to send
|
||||
/// over.
|
||||
void HTTP::Parser::StartResponse(HTTP::Parser &request, Socket::Connection &conn,
|
||||
bool bufferAllChunks){
|
||||
StartResponse("200", "OK", request, conn, bufferAllChunks);
|
||||
}
|
||||
|
||||
/// After receiving a header with this object, and after a call with SendResponse/SendRequest with this object, this function call will:
|
||||
/// After receiving a header with this object, and after a call with SendResponse/SendRequest with
|
||||
/// this object, this function call will:
|
||||
/// - Retrieve all the body from the 'from' Socket::Connection.
|
||||
/// - Forward those contents as-is to the 'to' Socket::Connection.
|
||||
/// It blocks until completed or either of the connections reaches an error state.
|
||||
|
@ -469,7 +522,9 @@ void HTTP::Parser::Proxy(Socket::Connection & from, Socket::Connection & to) {
|
|||
if (getChunks){
|
||||
unsigned int proxyingChunk = 0;
|
||||
while (to.connected() && from.connected()){
|
||||
if ((from.Received().size() && (from.Received().size() > 1 || *(from.Received().get().rbegin()) == '\n')) || from.spool()) {
|
||||
if ((from.Received().size() &&
|
||||
(from.Received().size() > 1 || *(from.Received().get().rbegin()) == '\n')) ||
|
||||
from.spool()){
|
||||
if (proxyingChunk){
|
||||
while (proxyingChunk && from.Received().size()){
|
||||
unsigned int toappend = from.Received().get().size();
|
||||
|
@ -497,17 +552,13 @@ void HTTP::Parser::Proxy(Socket::Connection & from, Socket::Connection & to) {
|
|||
}else{
|
||||
Util::sleep(100);
|
||||
}
|
||||
if (*(from.Received().get().rbegin()) != '\n') {
|
||||
continue;
|
||||
}
|
||||
if (*(from.Received().get().rbegin()) != '\n'){continue;}
|
||||
}
|
||||
// forward the size and any empty lines
|
||||
to.SendNow(from.Received().get());
|
||||
|
||||
std::string tmpA = from.Received().get().substr(0, from.Received().get().size() - 1);
|
||||
while (tmpA.find('\r') != std::string::npos) {
|
||||
tmpA.erase(tmpA.find('\r'));
|
||||
}
|
||||
while (tmpA.find('\r') != std::string::npos){tmpA.erase(tmpA.find('\r'));}
|
||||
unsigned int chunkLen = 0;
|
||||
if (!tmpA.empty()){
|
||||
for (unsigned int i = 0; i < tmpA.size(); ++i){
|
||||
|
@ -559,16 +610,15 @@ void HTTP::Parser::Trim(std::string & s) {
|
|||
}
|
||||
}
|
||||
|
||||
/// Function that sets the body of a response or request, along with the correct Content-Length header.
|
||||
/// \param s The string to set the body to.
|
||||
/// Function that sets the body of a response or request, along with the correct Content-Length
|
||||
/// header. \param s The string to set the body to.
|
||||
void HTTP::Parser::SetBody(std::string s){
|
||||
body = s;
|
||||
SetHeader("Content-Length", s.length());
|
||||
}
|
||||
|
||||
/// Function that sets the body of a response or request, along with the correct Content-Length header.
|
||||
/// \param buffer The buffer data to set the body to.
|
||||
/// \param len Length of the buffer data.
|
||||
/// Function that sets the body of a response or request, along with the correct Content-Length
|
||||
/// header. \param buffer The buffer data to set the body to. \param len Length of the buffer data.
|
||||
void HTTP::Parser::SetBody(const char *buffer, int len){
|
||||
body = "";
|
||||
body.append(buffer, len);
|
||||
|
@ -648,19 +698,17 @@ void HTTP::Parser::SetVar(std::string i, std::string v) {
|
|||
Trim(i);
|
||||
Trim(v);
|
||||
// only set if there is actually a key
|
||||
if (!i.empty()) {
|
||||
vars[i] = v;
|
||||
}
|
||||
if (!i.empty()){vars[i] = v;}
|
||||
}
|
||||
|
||||
/// Attempt to read a whole HTTP request or response from a Socket::Connection.
|
||||
/// If a whole request could be read, it is removed from the front of the socket buffer and true returned.
|
||||
/// If not, as much as can be interpreted is removed and false returned.
|
||||
/// \param conn The socket to read from.
|
||||
/// \return True if a whole request or response was read, false otherwise.
|
||||
/// If a whole request could be read, it is removed from the front of the socket buffer and true
|
||||
/// returned. If not, as much as can be interpreted is removed and false returned. \param conn The
|
||||
/// socket to read from. \return True if a whole request or response was read, false otherwise.
|
||||
bool HTTP::Parser::Read(Socket::Connection &conn){
|
||||
// Make sure the received data ends in a newline (\n).
|
||||
while ((!seenHeaders || (getChunks && !doingChunk)) && conn.Received().get().size() && *(conn.Received().get().rbegin()) != '\n') {
|
||||
while ((!seenHeaders || (getChunks && !doingChunk)) && conn.Received().get().size() &&
|
||||
*(conn.Received().get().rbegin()) != '\n'){
|
||||
if (conn.Received().size() > 1){
|
||||
// make a copy of the first part
|
||||
std::string tmp = conn.Received().get();
|
||||
|
@ -674,21 +722,16 @@ bool HTTP::Parser::Read(Socket::Connection & conn) {
|
|||
}
|
||||
}
|
||||
// if a parse succeeds, simply return true
|
||||
if (parse(conn.Received().get())) {
|
||||
return true;
|
||||
}
|
||||
if (parse(conn.Received().get())){return true;}
|
||||
// otherwise, if we have parts left, call ourselves recursively
|
||||
if (conn.Received().size()) {
|
||||
return Read(conn);
|
||||
}
|
||||
if (conn.Received().size()){return Read(conn);}
|
||||
return false;
|
||||
}// HTTPReader::Read
|
||||
|
||||
/// Attempt to read a whole HTTP request or response from a std::string buffer.
|
||||
/// If a whole request could be read, it is removed from the front of the given buffer and true returned.
|
||||
/// If not, as much as can be interpreted is removed and false returned.
|
||||
/// \param strbuf The buffer to read from.
|
||||
/// \return True if a whole request or response was read, false otherwise.
|
||||
/// If a whole request could be read, it is removed from the front of the given buffer and true
|
||||
/// returned. If not, as much as can be interpreted is removed and false returned. \param strbuf The
|
||||
/// buffer to read from. \return True if a whole request or response was read, false otherwise.
|
||||
bool HTTP::Parser::Read(std::string &strbuf){
|
||||
return parse(strbuf);
|
||||
}// HTTPReader::Read
|
||||
|
@ -701,7 +744,8 @@ bool HTTP::Parser::Read(std::string & strbuf) {
|
|||
bool HTTP::Parser::parse(std::string &HTTPbuffer){
|
||||
size_t f;
|
||||
std::string tmpA, tmpB, tmpC;
|
||||
/// \todo Make this not resize HTTPbuffer in parts, but read all at once and then remove the entire request, like doxygen claims it does?
|
||||
/// \todo Make this not resize HTTPbuffer in parts, but read all at once and then remove the
|
||||
/// entire request, like doxygen claims it does?
|
||||
while (!HTTPbuffer.empty()){
|
||||
if (!seenHeaders){
|
||||
f = HTTPbuffer.find('\n');
|
||||
|
@ -712,9 +756,7 @@ bool HTTP::Parser::parse(std::string & HTTPbuffer) {
|
|||
}else{
|
||||
HTTPbuffer.erase(0, f + 1);
|
||||
}
|
||||
while (tmpA.find('\r') != std::string::npos) {
|
||||
tmpA.erase(tmpA.find('\r'));
|
||||
}
|
||||
while (tmpA.find('\r') != std::string::npos){tmpA.erase(tmpA.find('\r'));}
|
||||
if (!seenReq){
|
||||
seenReq = true;
|
||||
f = tmpA.find(' ');
|
||||
|
@ -761,9 +803,7 @@ bool HTTP::Parser::parse(std::string & HTTPbuffer) {
|
|||
body.clear();
|
||||
if (GetHeader("Content-Length") != ""){
|
||||
length = atoi(GetHeader("Content-Length").c_str());
|
||||
if (body.capacity() < length) {
|
||||
body.reserve(length);
|
||||
}
|
||||
if (body.capacity() < length){body.reserve(length);}
|
||||
}
|
||||
if (GetHeader("Transfer-Encoding") == "chunked"){
|
||||
getChunks = true;
|
||||
|
@ -780,9 +820,7 @@ bool HTTP::Parser::parse(std::string & HTTPbuffer) {
|
|||
}
|
||||
if (seenHeaders){
|
||||
if (length > 0){
|
||||
if (headerOnly) {
|
||||
return true;
|
||||
}
|
||||
if (headerOnly){return true;}
|
||||
unsigned int toappend = length - body.length();
|
||||
if (toappend > 0){
|
||||
body.append(HTTPbuffer, 0, toappend);
|
||||
|
@ -796,14 +834,10 @@ bool HTTP::Parser::parse(std::string & HTTPbuffer) {
|
|||
}
|
||||
}else{
|
||||
if (getChunks){
|
||||
if (headerOnly) {
|
||||
return true;
|
||||
}
|
||||
if (headerOnly){return true;}
|
||||
if (doingChunk){
|
||||
unsigned int toappend = HTTPbuffer.size();
|
||||
if (toappend > doingChunk) {
|
||||
toappend = doingChunk;
|
||||
}
|
||||
if (toappend > doingChunk){toappend = doingChunk;}
|
||||
body.append(HTTPbuffer, 0, toappend);
|
||||
HTTPbuffer.erase(0, toappend);
|
||||
doingChunk -= toappend;
|
||||
|
@ -811,9 +845,7 @@ bool HTTP::Parser::parse(std::string & HTTPbuffer) {
|
|||
f = HTTPbuffer.find('\n');
|
||||
if (f == std::string::npos) return false;
|
||||
tmpA = HTTPbuffer.substr(0, f);
|
||||
while (tmpA.find('\r') != std::string::npos) {
|
||||
tmpA.erase(tmpA.find('\r'));
|
||||
}
|
||||
while (tmpA.find('\r') != std::string::npos){tmpA.erase(tmpA.find('\r'));}
|
||||
unsigned int chunkLen = 0;
|
||||
if (!tmpA.empty()){
|
||||
for (unsigned int i = 0; i < tmpA.size(); ++i){
|
||||
|
@ -850,9 +882,7 @@ void HTTP::parseVars(const std::string & data, std::map<std::string, std::string
|
|||
size_t pos = 0;
|
||||
while (pos < data.length()){
|
||||
size_t nextpos = data.find('&', pos);
|
||||
if (nextpos == std::string::npos) {
|
||||
nextpos = data.length();
|
||||
}
|
||||
if (nextpos == std::string::npos){nextpos = data.length();}
|
||||
size_t eq_pos = data.find('=', pos);
|
||||
if (eq_pos < nextpos){
|
||||
// there is a key and value
|
||||
|
@ -900,9 +930,7 @@ void HTTP::Parser::Chunkify(const char * data, unsigned int size, Socket::Connec
|
|||
}
|
||||
if (sendingChunks){
|
||||
// prepend the chunk size and \r\n
|
||||
if (!size){
|
||||
conn.SendNow("0\r\n\r\n", 5);
|
||||
}
|
||||
if (!size){conn.SendNow("0\r\n\r\n", 5);}
|
||||
size_t offset = 8;
|
||||
unsigned int t_size = size;
|
||||
char len[] = "\000\000\000\000\000\000\0000\r\n";
|
||||
|
|
|
@ -2,11 +2,11 @@
|
|||
/// Holds all headers for the HTTP namespace.
|
||||
|
||||
#pragma once
|
||||
#include <map>
|
||||
#include <string>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include "socket.h"
|
||||
#include <map>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string>
|
||||
|
||||
/// Holds all HTTP processing related code.
|
||||
namespace HTTP{
|
||||
|
@ -15,7 +15,6 @@ namespace HTTP {
|
|||
/// Reads variables from data, decodes and stores them to storage.
|
||||
void parseVars(const std::string &data, std::map<std::string, std::string> &storage);
|
||||
|
||||
|
||||
/// Simple class for reading and writing HTTP 1.0 and 1.1.
|
||||
class Parser{
|
||||
public:
|
||||
|
@ -37,16 +36,18 @@ namespace HTTP {
|
|||
std::string &BuildRequest();
|
||||
std::string &BuildResponse();
|
||||
std::string &BuildResponse(std::string code, std::string message);
|
||||
void SendRequest(Socket::Connection & conn);
|
||||
void SendRequest(Socket::Connection &conn, const std::string &reqbody = "");
|
||||
void SendResponse(std::string code, std::string message, Socket::Connection &conn);
|
||||
void StartResponse(std::string code, std::string message, Parser & request, Socket::Connection & conn, bool bufferAllChunks = false);
|
||||
void StartResponse(std::string code, std::string message, Parser &request,
|
||||
Socket::Connection &conn, bool bufferAllChunks = false);
|
||||
void StartResponse(Parser &request, Socket::Connection &conn, bool bufferAllChunks = false);
|
||||
void Chunkify(const std::string &bodypart, Socket::Connection &conn);
|
||||
void Chunkify(const char *data, unsigned int size, Socket::Connection &conn);
|
||||
void Proxy(Socket::Connection &from, Socket::Connection &to);
|
||||
void Clean();
|
||||
void CleanPreserveHeaders();
|
||||
void auth(const std::string & user, const std::string & pass, const std::string & authReq);
|
||||
void auth(const std::string &user, const std::string &pass, const std::string &authReq,
|
||||
const std::string &headerName = "Authorization");
|
||||
std::string body;
|
||||
std::string method;
|
||||
std::string url;
|
||||
|
@ -58,6 +59,7 @@ namespace HTTP {
|
|||
bool sendingChunks;
|
||||
|
||||
private:
|
||||
std::string cnonce;
|
||||
bool seenHeaders;
|
||||
bool seenReq;
|
||||
bool getChunks;
|
||||
|
@ -73,11 +75,12 @@ namespace HTTP {
|
|||
/// URL parsing class. Parses full URL into its subcomponents
|
||||
class URL{
|
||||
public:
|
||||
URL(const std::string & url);
|
||||
URL(const std::string &url = "");
|
||||
uint32_t getPort() const;
|
||||
uint32_t getDefaultPort() const;
|
||||
std::string getUrl() const;
|
||||
std::string getBareUrl() const;
|
||||
std::string getProxyUrl() const;
|
||||
std::string host; ///< Hostname or IP address of URL
|
||||
std::string protocol; ///< Protocol of URL
|
||||
std::string port; ///< Port of URL
|
||||
|
@ -90,4 +93,5 @@ namespace HTTP {
|
|||
bool IPv6Addr;
|
||||
};
|
||||
|
||||
}//HTTP namespace
|
||||
}// namespace HTTP
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue