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.
|
/// Holds all headers for the HTTP namespace.
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
#include <map>
|
|
||||||
#include <string>
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <stdio.h>
|
|
||||||
#include "socket.h"
|
#include "socket.h"
|
||||||
|
#include <map>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
/// Holds all HTTP processing related code.
|
/// Holds all HTTP processing related code.
|
||||||
namespace HTTP {
|
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);
|
|
||||||
|
|
||||||
|
/// 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.
|
/// Simple class for reading and writing HTTP 1.0 and 1.1.
|
||||||
class Parser {
|
class Parser{
|
||||||
public:
|
public:
|
||||||
Parser();
|
Parser();
|
||||||
bool Read(Socket::Connection & conn);
|
bool Read(Socket::Connection &conn);
|
||||||
bool Read(std::string & strbuf);
|
bool Read(std::string &strbuf);
|
||||||
const std::string & GetHeader(const std::string & i) const;
|
const std::string &GetHeader(const std::string &i) const;
|
||||||
bool hasHeader(const std::string & i) const;
|
bool hasHeader(const std::string &i) const;
|
||||||
void clearHeader(const std::string & i);
|
void clearHeader(const std::string &i);
|
||||||
const std::string & GetVar(const std::string & i) const;
|
const std::string &GetVar(const std::string &i) const;
|
||||||
std::string getUrl();
|
std::string getUrl();
|
||||||
std::string allVars();
|
std::string allVars();
|
||||||
void SetHeader(std::string i, std::string v);
|
void SetHeader(std::string i, std::string v);
|
||||||
void SetHeader(std::string i, long long v);
|
void SetHeader(std::string i, long long v);
|
||||||
void setCORSHeaders();
|
void setCORSHeaders();
|
||||||
void SetVar(std::string i, std::string v);
|
void SetVar(std::string i, std::string v);
|
||||||
void SetBody(std::string s);
|
void SetBody(std::string s);
|
||||||
void SetBody(const char * buffer, int len);
|
void SetBody(const char *buffer, int len);
|
||||||
std::string & BuildRequest();
|
std::string &BuildRequest();
|
||||||
std::string & BuildResponse();
|
std::string &BuildResponse();
|
||||||
std::string & BuildResponse(std::string code, std::string message);
|
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 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,
|
||||||
void StartResponse(Parser & request, Socket::Connection & conn, bool bufferAllChunks = false);
|
Socket::Connection &conn, bool bufferAllChunks = false);
|
||||||
void Chunkify(const std::string & bodypart, Socket::Connection & conn);
|
void StartResponse(Parser &request, Socket::Connection &conn, bool bufferAllChunks = false);
|
||||||
void Chunkify(const char * data, unsigned int size, Socket::Connection & conn);
|
void Chunkify(const std::string &bodypart, Socket::Connection &conn);
|
||||||
void Proxy(Socket::Connection & from, Socket::Connection & to);
|
void Chunkify(const char *data, unsigned int size, Socket::Connection &conn);
|
||||||
void Clean();
|
void Proxy(Socket::Connection &from, Socket::Connection &to);
|
||||||
void CleanPreserveHeaders();
|
void Clean();
|
||||||
void auth(const std::string & user, const std::string & pass, const std::string & authReq);
|
void CleanPreserveHeaders();
|
||||||
std::string body;
|
void auth(const std::string &user, const std::string &pass, const std::string &authReq,
|
||||||
std::string method;
|
const std::string &headerName = "Authorization");
|
||||||
std::string url;
|
std::string body;
|
||||||
std::string protocol;
|
std::string method;
|
||||||
unsigned int length;
|
std::string url;
|
||||||
bool headerOnly; ///< If true, do not parse body if the length is a known size.
|
std::string protocol;
|
||||||
bool bufferChunks;
|
unsigned int length;
|
||||||
//this bool was private
|
bool headerOnly; ///< If true, do not parse body if the length is a known size.
|
||||||
bool sendingChunks;
|
bool bufferChunks;
|
||||||
|
// this bool was private
|
||||||
|
bool sendingChunks;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
bool seenHeaders;
|
std::string cnonce;
|
||||||
bool seenReq;
|
bool seenHeaders;
|
||||||
bool getChunks;
|
bool seenReq;
|
||||||
unsigned int doingChunk;
|
bool getChunks;
|
||||||
bool parse(std::string & HTTPbuffer);
|
unsigned int doingChunk;
|
||||||
std::string builder;
|
bool parse(std::string &HTTPbuffer);
|
||||||
std::string read_buffer;
|
std::string builder;
|
||||||
std::map<std::string, std::string> headers;
|
std::string read_buffer;
|
||||||
std::map<std::string, std::string> vars;
|
std::map<std::string, std::string> headers;
|
||||||
void Trim(std::string & s);
|
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;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
}//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