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
File diff suppressed because it is too large
Load diff
|
@ -2,92 +2,96 @@
|
|||
/// 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 {
|
||||
|
||||
///HTTP variable parser to std::map<std::string, std::string> structure.
|
||||
///Reads variables from data, decodes and stores them to storage.
|
||||
void parseVars(const std::string & data, std::map<std::string, std::string> & storage);
|
||||
namespace HTTP{
|
||||
|
||||
/// HTTP variable parser to std::map<std::string, std::string> structure.
|
||||
/// 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:
|
||||
Parser();
|
||||
bool Read(Socket::Connection & conn);
|
||||
bool Read(std::string & strbuf);
|
||||
const std::string & GetHeader(const std::string & i) const;
|
||||
bool hasHeader(const std::string & i) const;
|
||||
void clearHeader(const std::string & i);
|
||||
const std::string & GetVar(const std::string & i) const;
|
||||
std::string getUrl();
|
||||
std::string allVars();
|
||||
void SetHeader(std::string i, std::string v);
|
||||
void SetHeader(std::string i, long long v);
|
||||
void setCORSHeaders();
|
||||
void SetVar(std::string i, std::string v);
|
||||
void SetBody(std::string s);
|
||||
void SetBody(const char * buffer, int len);
|
||||
std::string & BuildRequest();
|
||||
std::string & BuildResponse();
|
||||
std::string & BuildResponse(std::string code, std::string message);
|
||||
void SendRequest(Socket::Connection & conn);
|
||||
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(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);
|
||||
std::string body;
|
||||
std::string method;
|
||||
std::string url;
|
||||
std::string protocol;
|
||||
unsigned int length;
|
||||
bool headerOnly; ///< If true, do not parse body if the length is a known size.
|
||||
bool bufferChunks;
|
||||
//this bool was private
|
||||
bool sendingChunks;
|
||||
class Parser{
|
||||
public:
|
||||
Parser();
|
||||
bool Read(Socket::Connection &conn);
|
||||
bool Read(std::string &strbuf);
|
||||
const std::string &GetHeader(const std::string &i) const;
|
||||
bool hasHeader(const std::string &i) const;
|
||||
void clearHeader(const std::string &i);
|
||||
const std::string &GetVar(const std::string &i) const;
|
||||
std::string getUrl();
|
||||
std::string allVars();
|
||||
void SetHeader(std::string i, std::string v);
|
||||
void SetHeader(std::string i, long long v);
|
||||
void setCORSHeaders();
|
||||
void SetVar(std::string i, std::string v);
|
||||
void SetBody(std::string s);
|
||||
void SetBody(const char *buffer, int len);
|
||||
std::string &BuildRequest();
|
||||
std::string &BuildResponse();
|
||||
std::string &BuildResponse(std::string code, std::string message);
|
||||
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(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,
|
||||
const std::string &headerName = "Authorization");
|
||||
std::string body;
|
||||
std::string method;
|
||||
std::string url;
|
||||
std::string protocol;
|
||||
unsigned int length;
|
||||
bool headerOnly; ///< If true, do not parse body if the length is a known size.
|
||||
bool bufferChunks;
|
||||
// this bool was private
|
||||
bool sendingChunks;
|
||||
|
||||
private:
|
||||
bool seenHeaders;
|
||||
bool seenReq;
|
||||
bool getChunks;
|
||||
unsigned int doingChunk;
|
||||
bool parse(std::string & HTTPbuffer);
|
||||
std::string builder;
|
||||
std::string read_buffer;
|
||||
std::map<std::string, std::string> headers;
|
||||
std::map<std::string, std::string> vars;
|
||||
void Trim(std::string & s);
|
||||
};
|
||||
|
||||
///URL parsing class. Parses full URL into its subcomponents
|
||||
class URL {
|
||||
public:
|
||||
URL(const std::string & url);
|
||||
uint32_t getPort() const;
|
||||
uint32_t getDefaultPort() const;
|
||||
std::string getUrl() const;
|
||||
std::string getBareUrl() const;
|
||||
std::string host;///< Hostname or IP address of URL
|
||||
std::string protocol;///<Protocol of URL
|
||||
std::string port;///<Port of URL
|
||||
std::string path;///<Path after the first slash (not inclusive) but before any question mark
|
||||
std::string args;///<Everything after the question mark in the path, if it was present
|
||||
std::string frag;///<Everything after the # in the path, if it was present
|
||||
std::string user;///<Username, if it was present
|
||||
std::string pass;///<Password, if it was present
|
||||
URL link(const std::string &l) const;
|
||||
bool IPv6Addr;
|
||||
private:
|
||||
std::string cnonce;
|
||||
bool seenHeaders;
|
||||
bool seenReq;
|
||||
bool getChunks;
|
||||
unsigned int doingChunk;
|
||||
bool parse(std::string &HTTPbuffer);
|
||||
std::string builder;
|
||||
std::string read_buffer;
|
||||
std::map<std::string, std::string> headers;
|
||||
std::map<std::string, std::string> vars;
|
||||
void Trim(std::string &s);
|
||||
};
|
||||
|
||||
}//HTTP namespace
|
||||
/// URL parsing class. Parses full URL into its subcomponents
|
||||
class URL{
|
||||
public:
|
||||
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
|
||||
std::string path; ///< Path after the first slash (not inclusive) but before any question mark
|
||||
std::string args; ///< Everything after the question mark in the path, if it was present
|
||||
std::string frag; ///< Everything after the # in the path, if it was present
|
||||
std::string user; ///< Username, if it was present
|
||||
std::string pass; ///< Password, if it was present
|
||||
URL link(const std::string &l) const;
|
||||
bool IPv6Addr;
|
||||
};
|
||||
|
||||
}// namespace HTTP
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue