Merge branch 'development' into LTS_development
# Conflicts: # lib/http_parser.h
This commit is contained in:
commit
f910e3dc04
2 changed files with 410 additions and 378 deletions
|
@ -2,10 +2,11 @@
|
||||||
/// Holds all code for the HTTP namespace.
|
/// Holds all code for the HTTP namespace.
|
||||||
|
|
||||||
#include "http_parser.h"
|
#include "http_parser.h"
|
||||||
|
#include "auth.h"
|
||||||
|
#include "defines.h"
|
||||||
#include "encode.h"
|
#include "encode.h"
|
||||||
#include "timing.h"
|
#include "timing.h"
|
||||||
#include "defines.h"
|
#include <iomanip>
|
||||||
#include "auth.h"
|
|
||||||
|
|
||||||
/// Helper function to check if the given c-string is numeric or not
|
/// Helper function to check if the given c-string is numeric or not
|
||||||
static bool is_numeric(const char *str){
|
static bool is_numeric(const char *str){
|
||||||
|
@ -26,9 +27,7 @@ HTTP::URL::URL(const std::string & url){
|
||||||
proto_sep += 3;
|
proto_sep += 3;
|
||||||
}else{
|
}else{
|
||||||
proto_sep = 0;
|
proto_sep = 0;
|
||||||
if (url.substr(0, 2) == "//"){
|
if (url.substr(0, 2) == "//"){proto_sep = 2;}
|
||||||
proto_sep = 2;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
// proto_sep now points to the start of the host, guaranteed
|
// proto_sep now points to the start of the host, guaranteed
|
||||||
// continue by finding the path, if any
|
// continue by finding the path, if any
|
||||||
|
@ -50,9 +49,7 @@ HTTP::URL::URL(const std::string & url){
|
||||||
path.erase(qmark);
|
path.erase(qmark);
|
||||||
}
|
}
|
||||||
if (path.size()){
|
if (path.size()){
|
||||||
if (path[0] == '/'){
|
if (path[0] == '/'){path.erase(0, 1);}
|
||||||
path.erase(0, 1);
|
|
||||||
}
|
|
||||||
size_t dots = path.find("/./");
|
size_t dots = path.find("/./");
|
||||||
while (dots != std::string::npos){
|
while (dots != std::string::npos){
|
||||||
DONTEVEN_MSG("%s (/./ -> /)", path.c_str());
|
DONTEVEN_MSG("%s (/./ -> /)", path.c_str());
|
||||||
|
@ -65,9 +62,7 @@ HTTP::URL::URL(const std::string & url){
|
||||||
path.erase(dots, 1);
|
path.erase(dots, 1);
|
||||||
dots = path.find("//");
|
dots = path.find("//");
|
||||||
}
|
}
|
||||||
if (path[0] == '/'){
|
if (path[0] == '/'){path.erase(0, 1);}
|
||||||
path.erase(0, 1);
|
|
||||||
}
|
|
||||||
dots = path.find("/../");
|
dots = path.find("/../");
|
||||||
while (dots != std::string::npos){
|
while (dots != std::string::npos){
|
||||||
size_t prevslash = path.rfind('/', dots - 1);
|
size_t prevslash = path.rfind('/', dots - 1);
|
||||||
|
@ -78,12 +73,8 @@ HTTP::URL::URL(const std::string & url){
|
||||||
}
|
}
|
||||||
dots = path.find("/../");
|
dots = path.find("/../");
|
||||||
}
|
}
|
||||||
if (path.substr(0, 2) == "./"){
|
if (path.substr(0, 2) == "./"){path.erase(0, 2);}
|
||||||
path.erase(0, 2);
|
if (path.substr(0, 3) == "../"){path.erase(0, 3);}
|
||||||
}
|
|
||||||
if (path.substr(0, 3) == "../"){
|
|
||||||
path.erase(0, 3);
|
|
||||||
}
|
|
||||||
path = Encodings::URL::decode(path);
|
path = Encodings::URL::decode(path);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -187,6 +178,26 @@ std::string HTTP::URL::getUrl() const{
|
||||||
return ret;
|
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
|
/// Returns the URL in string format without args and frag
|
||||||
std::string HTTP::URL::getBareUrl() const{
|
std::string HTTP::URL::getBareUrl() const{
|
||||||
std::string ret;
|
std::string ret;
|
||||||
|
@ -248,15 +259,20 @@ HTTP::URL HTTP::URL::link(const std::string &l) const{
|
||||||
HTTP::Parser::Parser(){
|
HTTP::Parser::Parser(){
|
||||||
headerOnly = false;
|
headerOnly = false;
|
||||||
Clean();
|
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(){
|
void HTTP::Parser::Clean(){
|
||||||
CleanPreserveHeaders();
|
CleanPreserveHeaders();
|
||||||
headers.clear();
|
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(){
|
void HTTP::Parser::CleanPreserveHeaders(){
|
||||||
seenHeaders = false;
|
seenHeaders = false;
|
||||||
seenReq = false;
|
seenReq = false;
|
||||||
|
@ -274,17 +290,25 @@ void HTTP::Parser::CleanPreserveHeaders() {
|
||||||
/// Local-only helper function for use in auth()
|
/// Local-only helper function for use in auth()
|
||||||
/// Returns the string contents of the given val from list
|
/// Returns the string contents of the given val from list
|
||||||
static std::string findValIn(const std::string &list, const std::string &val){
|
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 "";}
|
if (pos == std::string::npos){return "";}
|
||||||
pos += val.size() + 2;
|
pos += val.size() + 1;
|
||||||
if (pos >= list.size()){return "";}
|
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 "";}
|
if (ePos == std::string::npos){return "";}
|
||||||
|
}else{
|
||||||
|
ePos = list.find(',', pos);
|
||||||
|
}
|
||||||
return list.substr(pos, ePos - 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.
|
/// Attempts to send an authentication header with the given name and password. Uses authReq as
|
||||||
void HTTP::Parser::auth(const std::string & user, const std::string & pass, const std::string & authReq){
|
/// 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(' ');
|
size_t space = authReq.find(' ');
|
||||||
if (space == std::string::npos || !user.size() || !pass.size()){
|
if (space == std::string::npos || !user.size() || !pass.size()){
|
||||||
FAIL_MSG("No authentication possible");
|
FAIL_MSG("No authentication possible");
|
||||||
|
@ -292,15 +316,48 @@ void HTTP::Parser::auth(const std::string & user, const std::string & pass, cons
|
||||||
}
|
}
|
||||||
std::string meth = authReq.substr(0, space);
|
std::string meth = authReq.substr(0, space);
|
||||||
if (meth == "Basic"){
|
if (meth == "Basic"){
|
||||||
SetHeader("Authorization", "Basic "+Encodings::Base64::encode(user+":"+pass));
|
SetHeader(headerName, "Basic " + Encodings::Base64::encode(user + ":" + pass));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (meth == "Digest"){
|
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 A1 = Secure::md5(user + ":" + realm + ":" + pass);
|
||||||
std::string A2 = Secure::md5(method+":"+url);
|
if (algo.size() && algo != "MD5"){
|
||||||
std::string response = Secure::md5(A1+":"+nonce+":"+A2);
|
FAIL_MSG("Authorization algorithm %s not implemented", algo.c_str());
|
||||||
SetHeader("Authorization", "Digest username=\""+user+"\", realm=\""+realm+"\", nonce=\""+nonce+"\", uri=\""+url+"\", response=\""+response+"\"");
|
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;
|
return;
|
||||||
}
|
}
|
||||||
FAIL_MSG("No authentication possible, unimplemented method '%s'", meth.c_str());
|
FAIL_MSG("No authentication possible, unimplemented method '%s'", meth.c_str());
|
||||||
|
@ -321,7 +378,6 @@ void HTTP::Parser::setCORSHeaders(){
|
||||||
SetHeader("Expires", "0");
|
SetHeader("Expires", "0");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/// Returns a string containing a valid HTTP 1.0 or 1.1 request, ready for sending.
|
/// 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.
|
/// The request is build from internal variables set before this call is made.
|
||||||
/// To be precise, method, url, protocol, headers and body are used.
|
/// To be precise, method, url, protocol, headers and body are used.
|
||||||
|
@ -329,9 +385,7 @@ void HTTP::Parser::setCORSHeaders(){
|
||||||
std::string &HTTP::Parser::BuildRequest(){
|
std::string &HTTP::Parser::BuildRequest(){
|
||||||
/// \todo Include GET/POST variable parsing?
|
/// \todo Include GET/POST variable parsing?
|
||||||
std::map<std::string, std::string>::iterator it;
|
std::map<std::string, std::string>::iterator it;
|
||||||
if (protocol.size() < 5 || protocol[4] != '/') {
|
if (protocol.size() < 5 || protocol[4] != '/'){protocol = "HTTP/1.0";}
|
||||||
protocol = "HTTP/1.0";
|
|
||||||
}
|
|
||||||
builder = method + " " + url + " " + protocol + "\r\n";
|
builder = method + " " + url + " " + protocol + "\r\n";
|
||||||
for (it = headers.begin(); it != headers.end(); it++){
|
for (it = headers.begin(); it != headers.end(); it++){
|
||||||
if ((*it).first != "" && (*it).second != ""){
|
if ((*it).first != "" && (*it).second != ""){
|
||||||
|
@ -345,14 +399,13 @@ std::string & HTTP::Parser::BuildRequest() {
|
||||||
/// Creates and sends a valid HTTP 1.0 or 1.1 request.
|
/// 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.
|
/// The request is build from internal variables set before this call is made.
|
||||||
/// To be precise, method, url, protocol, headers and body are used.
|
/// 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?
|
/// \todo Include GET/POST variable parsing?
|
||||||
std::map<std::string, std::string>::iterator it;
|
std::map<std::string, std::string>::iterator it;
|
||||||
if (protocol.size() < 5 || protocol[4] != '/') {
|
if (protocol.size() < 5 || protocol[4] != '/'){protocol = "HTTP/1.0";}
|
||||||
protocol = "HTTP/1.0";
|
|
||||||
}
|
|
||||||
builder = method + " " + url + " " + protocol + "\r\n";
|
builder = method + " " + url + " " + protocol + "\r\n";
|
||||||
conn.SendNow(builder);
|
conn.SendNow(builder);
|
||||||
|
if (reqbody.size()){SetHeader("Content-Length", reqbody.length());}
|
||||||
for (it = headers.begin(); it != headers.end(); it++){
|
for (it = headers.begin(); it != headers.end(); it++){
|
||||||
if ((*it).first != "" && (*it).second != ""){
|
if ((*it).first != "" && (*it).second != ""){
|
||||||
builder = (*it).first + ": " + (*it).second + "\r\n";
|
builder = (*it).first + ": " + (*it).second + "\r\n";
|
||||||
|
@ -360,8 +413,12 @@ void HTTP::Parser::SendRequest(Socket::Connection & conn) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
conn.SendNow("\r\n", 2);
|
conn.SendNow("\r\n", 2);
|
||||||
|
if (reqbody.size()){
|
||||||
|
conn.SendNow(reqbody);
|
||||||
|
}else{
|
||||||
conn.SendNow(body);
|
conn.SendNow(body);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Returns a string containing a valid HTTP 1.0 or 1.1 response, ready for sending.
|
/// 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.
|
/// The response is partly build from internal variables set before this call is made.
|
||||||
|
@ -372,9 +429,7 @@ void HTTP::Parser::SendRequest(Socket::Connection & conn) {
|
||||||
std::string &HTTP::Parser::BuildResponse(std::string code, std::string message){
|
std::string &HTTP::Parser::BuildResponse(std::string code, std::string message){
|
||||||
/// \todo Include GET/POST variable parsing?
|
/// \todo Include GET/POST variable parsing?
|
||||||
std::map<std::string, std::string>::iterator it;
|
std::map<std::string, std::string>::iterator it;
|
||||||
if (protocol.size() < 5 || protocol[4] != '/') {
|
if (protocol.size() < 5 || protocol[4] != '/'){protocol = "HTTP/1.0";}
|
||||||
protocol = "HTTP/1.0";
|
|
||||||
}
|
|
||||||
builder = protocol + " " + code + " " + message + "\r\n";
|
builder = protocol + " " + code + " " + message + "\r\n";
|
||||||
for (it = headers.begin(); it != headers.end(); it++){
|
for (it = headers.begin(); it != headers.end(); it++){
|
||||||
if ((*it).first != "" && (*it).second != ""){
|
if ((*it).first != "" && (*it).second != ""){
|
||||||
|
@ -400,16 +455,13 @@ std::string & HTTP::Parser::BuildResponse() {
|
||||||
/// Creates and sends a valid HTTP 1.0 or 1.1 response.
|
/// 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.
|
/// The response is partly build from internal variables set before this call is made.
|
||||||
/// To be precise, protocol, headers and body are used.
|
/// 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.
|
/// This call will attempt to buffer as little as possible and block until the whole request is
|
||||||
/// \param code The HTTP response code. Usually you want 200.
|
/// sent. \param code The HTTP response code. Usually you want 200. \param message The HTTP response
|
||||||
/// \param message The HTTP response message. Usually you want "OK".
|
/// message. Usually you want "OK". \param conn The Socket::Connection to send the response over.
|
||||||
/// \param conn The Socket::Connection to send the response over.
|
|
||||||
void HTTP::Parser::SendResponse(std::string code, std::string message, Socket::Connection &conn){
|
void HTTP::Parser::SendResponse(std::string code, std::string message, Socket::Connection &conn){
|
||||||
/// \todo Include GET/POST variable parsing?
|
/// \todo Include GET/POST variable parsing?
|
||||||
std::map<std::string, std::string>::iterator it;
|
std::map<std::string, std::string>::iterator it;
|
||||||
if (protocol.size() < 5 || protocol[4] != '/') {
|
if (protocol.size() < 5 || protocol[4] != '/'){protocol = "HTTP/1.0";}
|
||||||
protocol = "HTTP/1.0";
|
|
||||||
}
|
|
||||||
builder = protocol + " " + code + " " + message + "\r\n";
|
builder = protocol + " " + code + " " + message + "\r\n";
|
||||||
conn.SendNow(builder);
|
conn.SendNow(builder);
|
||||||
for (it = headers.begin(); it != headers.end(); it++){
|
for (it = headers.begin(); it != headers.end(); it++){
|
||||||
|
@ -426,14 +478,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.
|
/// 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.
|
/// 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 sets up chunked transfer encoding if the request was protocol HTTP/1.1, otherwise uses
|
||||||
/// \param code The HTTP response code. Usually you want 200.
|
/// a zero-content-length HTTP/1.0 response. \param code The HTTP response code. Usually you want
|
||||||
/// \param message The HTTP response message. Usually you want "OK".
|
/// 200. \param message The HTTP response message. Usually you want "OK". \param request The HTTP
|
||||||
/// \param request The HTTP request to respond to.
|
/// request to respond to. \param conn The connection to send over.
|
||||||
/// \param conn The connection to send over.
|
void HTTP::Parser::StartResponse(std::string code, std::string message, HTTP::Parser &request,
|
||||||
void HTTP::Parser::StartResponse(std::string code, std::string message, HTTP::Parser & request, Socket::Connection & conn, bool bufferAllChunks) {
|
Socket::Connection &conn, bool bufferAllChunks){
|
||||||
std::string prot = request.protocol;
|
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();
|
CleanPreserveHeaders();
|
||||||
protocol = prot;
|
protocol = prot;
|
||||||
if (sendingChunks){
|
if (sendingChunks){
|
||||||
|
@ -442,22 +495,22 @@ void HTTP::Parser::StartResponse(std::string code, std::string message, HTTP::Pa
|
||||||
SetHeader("Connection", "close");
|
SetHeader("Connection", "close");
|
||||||
}
|
}
|
||||||
bufferChunks = bufferAllChunks;
|
bufferChunks = bufferAllChunks;
|
||||||
if (!bufferAllChunks){
|
if (!bufferAllChunks){SendResponse(code, message, conn);}
|
||||||
SendResponse(code, message, conn);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Creates and sends a valid HTTP 1.0 or 1.1 response, based on the given request.
|
/// 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.
|
/// 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 sets up chunked transfer encoding if the request was protocol HTTP/1.1, otherwise uses
|
||||||
/// This call simply calls StartResponse("200", "OK", request, conn)
|
/// a zero-content-length HTTP/1.0 response. This call simply calls StartResponse("200", "OK",
|
||||||
/// \param request The HTTP request to respond to.
|
/// request, conn) \param request The HTTP request to respond to. \param conn The connection to send
|
||||||
/// \param conn The connection to send over.
|
/// over.
|
||||||
void HTTP::Parser::StartResponse(HTTP::Parser & request, Socket::Connection & conn, bool bufferAllChunks) {
|
void HTTP::Parser::StartResponse(HTTP::Parser &request, Socket::Connection &conn,
|
||||||
|
bool bufferAllChunks){
|
||||||
StartResponse("200", "OK", request, conn, 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.
|
/// - Retrieve all the body from the 'from' Socket::Connection.
|
||||||
/// - Forward those contents as-is to the 'to' 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.
|
/// It blocks until completed or either of the connections reaches an error state.
|
||||||
|
@ -465,7 +518,9 @@ void HTTP::Parser::Proxy(Socket::Connection & from, Socket::Connection & to) {
|
||||||
if (getChunks){
|
if (getChunks){
|
||||||
unsigned int proxyingChunk = 0;
|
unsigned int proxyingChunk = 0;
|
||||||
while (to.connected() && from.connected()){
|
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){
|
if (proxyingChunk){
|
||||||
while (proxyingChunk && from.Received().size()){
|
while (proxyingChunk && from.Received().size()){
|
||||||
unsigned int toappend = from.Received().get().size();
|
unsigned int toappend = from.Received().get().size();
|
||||||
|
@ -493,17 +548,13 @@ void HTTP::Parser::Proxy(Socket::Connection & from, Socket::Connection & to) {
|
||||||
}else{
|
}else{
|
||||||
Util::sleep(100);
|
Util::sleep(100);
|
||||||
}
|
}
|
||||||
if (*(from.Received().get().rbegin()) != '\n') {
|
if (*(from.Received().get().rbegin()) != '\n'){continue;}
|
||||||
continue;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
// forward the size and any empty lines
|
// forward the size and any empty lines
|
||||||
to.SendNow(from.Received().get());
|
to.SendNow(from.Received().get());
|
||||||
|
|
||||||
std::string tmpA = from.Received().get().substr(0, from.Received().get().size() - 1);
|
std::string tmpA = from.Received().get().substr(0, from.Received().get().size() - 1);
|
||||||
while (tmpA.find('\r') != std::string::npos) {
|
while (tmpA.find('\r') != std::string::npos){tmpA.erase(tmpA.find('\r'));}
|
||||||
tmpA.erase(tmpA.find('\r'));
|
|
||||||
}
|
|
||||||
unsigned int chunkLen = 0;
|
unsigned int chunkLen = 0;
|
||||||
if (!tmpA.empty()){
|
if (!tmpA.empty()){
|
||||||
for (unsigned int i = 0; i < tmpA.size(); ++i){
|
for (unsigned int i = 0; i < tmpA.size(); ++i){
|
||||||
|
@ -555,16 +606,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.
|
/// Function that sets the body of a response or request, along with the correct Content-Length
|
||||||
/// \param s The string to set the body to.
|
/// header. \param s The string to set the body to.
|
||||||
void HTTP::Parser::SetBody(std::string s){
|
void HTTP::Parser::SetBody(std::string s){
|
||||||
body = s;
|
body = s;
|
||||||
SetHeader("Content-Length", s.length());
|
SetHeader("Content-Length", s.length());
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Function that sets the body of a response or request, along with the correct Content-Length header.
|
/// Function that sets the body of a response or request, along with the correct Content-Length
|
||||||
/// \param buffer The buffer data to set the body to.
|
/// header. \param buffer The buffer data to set the body to. \param len Length of the buffer data.
|
||||||
/// \param len Length of the buffer data.
|
|
||||||
void HTTP::Parser::SetBody(const char *buffer, int len){
|
void HTTP::Parser::SetBody(const char *buffer, int len){
|
||||||
body = "";
|
body = "";
|
||||||
body.append(buffer, len);
|
body.append(buffer, len);
|
||||||
|
@ -644,19 +694,17 @@ void HTTP::Parser::SetVar(std::string i, std::string v) {
|
||||||
Trim(i);
|
Trim(i);
|
||||||
Trim(v);
|
Trim(v);
|
||||||
// only set if there is actually a key
|
// only set if there is actually a key
|
||||||
if (!i.empty()) {
|
if (!i.empty()){vars[i] = v;}
|
||||||
vars[i] = v;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Attempt to read a whole HTTP request or response from a Socket::Connection.
|
/// 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 a whole request could be read, it is removed from the front of the socket buffer and true
|
||||||
/// If not, as much as can be interpreted is removed and false returned.
|
/// returned. If not, as much as can be interpreted is removed and false returned. \param conn The
|
||||||
/// \param conn The socket to read from.
|
/// socket to read from. \return True if a whole request or response was read, false otherwise.
|
||||||
/// \return True if a whole request or response was read, false otherwise.
|
|
||||||
bool HTTP::Parser::Read(Socket::Connection &conn){
|
bool HTTP::Parser::Read(Socket::Connection &conn){
|
||||||
// Make sure the received data ends in a newline (\n).
|
// 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){
|
if (conn.Received().size() > 1){
|
||||||
// make a copy of the first part
|
// make a copy of the first part
|
||||||
std::string tmp = conn.Received().get();
|
std::string tmp = conn.Received().get();
|
||||||
|
@ -670,21 +718,16 @@ bool HTTP::Parser::Read(Socket::Connection & conn) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// if a parse succeeds, simply return true
|
// if a parse succeeds, simply return true
|
||||||
if (parse(conn.Received().get())) {
|
if (parse(conn.Received().get())){return true;}
|
||||||
return true;
|
|
||||||
}
|
|
||||||
// otherwise, if we have parts left, call ourselves recursively
|
// otherwise, if we have parts left, call ourselves recursively
|
||||||
if (conn.Received().size()) {
|
if (conn.Received().size()){return Read(conn);}
|
||||||
return Read(conn);
|
|
||||||
}
|
|
||||||
return false;
|
return false;
|
||||||
}// HTTPReader::Read
|
}// HTTPReader::Read
|
||||||
|
|
||||||
/// Attempt to read a whole HTTP request or response from a std::string buffer.
|
/// 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 a whole request could be read, it is removed from the front of the given buffer and true
|
||||||
/// If not, as much as can be interpreted is removed and false returned.
|
/// returned. If not, as much as can be interpreted is removed and false returned. \param strbuf The
|
||||||
/// \param strbuf The buffer to read from.
|
/// buffer to read from. \return True if a whole request or response was read, false otherwise.
|
||||||
/// \return True if a whole request or response was read, false otherwise.
|
|
||||||
bool HTTP::Parser::Read(std::string &strbuf){
|
bool HTTP::Parser::Read(std::string &strbuf){
|
||||||
return parse(strbuf);
|
return parse(strbuf);
|
||||||
}// HTTPReader::Read
|
}// HTTPReader::Read
|
||||||
|
@ -697,7 +740,8 @@ bool HTTP::Parser::Read(std::string & strbuf) {
|
||||||
bool HTTP::Parser::parse(std::string &HTTPbuffer){
|
bool HTTP::Parser::parse(std::string &HTTPbuffer){
|
||||||
size_t f;
|
size_t f;
|
||||||
std::string tmpA, tmpB, tmpC;
|
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()){
|
while (!HTTPbuffer.empty()){
|
||||||
if (!seenHeaders){
|
if (!seenHeaders){
|
||||||
f = HTTPbuffer.find('\n');
|
f = HTTPbuffer.find('\n');
|
||||||
|
@ -708,9 +752,7 @@ bool HTTP::Parser::parse(std::string & HTTPbuffer) {
|
||||||
}else{
|
}else{
|
||||||
HTTPbuffer.erase(0, f + 1);
|
HTTPbuffer.erase(0, f + 1);
|
||||||
}
|
}
|
||||||
while (tmpA.find('\r') != std::string::npos) {
|
while (tmpA.find('\r') != std::string::npos){tmpA.erase(tmpA.find('\r'));}
|
||||||
tmpA.erase(tmpA.find('\r'));
|
|
||||||
}
|
|
||||||
if (!seenReq){
|
if (!seenReq){
|
||||||
seenReq = true;
|
seenReq = true;
|
||||||
f = tmpA.find(' ');
|
f = tmpA.find(' ');
|
||||||
|
@ -757,9 +799,7 @@ bool HTTP::Parser::parse(std::string & HTTPbuffer) {
|
||||||
body.clear();
|
body.clear();
|
||||||
if (GetHeader("Content-Length") != ""){
|
if (GetHeader("Content-Length") != ""){
|
||||||
length = atoi(GetHeader("Content-Length").c_str());
|
length = atoi(GetHeader("Content-Length").c_str());
|
||||||
if (body.capacity() < length) {
|
if (body.capacity() < length){body.reserve(length);}
|
||||||
body.reserve(length);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
if (GetHeader("Transfer-Encoding") == "chunked"){
|
if (GetHeader("Transfer-Encoding") == "chunked"){
|
||||||
getChunks = true;
|
getChunks = true;
|
||||||
|
@ -776,9 +816,7 @@ bool HTTP::Parser::parse(std::string & HTTPbuffer) {
|
||||||
}
|
}
|
||||||
if (seenHeaders){
|
if (seenHeaders){
|
||||||
if (length > 0){
|
if (length > 0){
|
||||||
if (headerOnly) {
|
if (headerOnly){return true;}
|
||||||
return true;
|
|
||||||
}
|
|
||||||
unsigned int toappend = length - body.length();
|
unsigned int toappend = length - body.length();
|
||||||
if (toappend > 0){
|
if (toappend > 0){
|
||||||
body.append(HTTPbuffer, 0, toappend);
|
body.append(HTTPbuffer, 0, toappend);
|
||||||
|
@ -792,14 +830,10 @@ bool HTTP::Parser::parse(std::string & HTTPbuffer) {
|
||||||
}
|
}
|
||||||
}else{
|
}else{
|
||||||
if (getChunks){
|
if (getChunks){
|
||||||
if (headerOnly) {
|
if (headerOnly){return true;}
|
||||||
return true;
|
|
||||||
}
|
|
||||||
if (doingChunk){
|
if (doingChunk){
|
||||||
unsigned int toappend = HTTPbuffer.size();
|
unsigned int toappend = HTTPbuffer.size();
|
||||||
if (toappend > doingChunk) {
|
if (toappend > doingChunk){toappend = doingChunk;}
|
||||||
toappend = doingChunk;
|
|
||||||
}
|
|
||||||
body.append(HTTPbuffer, 0, toappend);
|
body.append(HTTPbuffer, 0, toappend);
|
||||||
HTTPbuffer.erase(0, toappend);
|
HTTPbuffer.erase(0, toappend);
|
||||||
doingChunk -= toappend;
|
doingChunk -= toappend;
|
||||||
|
@ -807,9 +841,7 @@ bool HTTP::Parser::parse(std::string & HTTPbuffer) {
|
||||||
f = HTTPbuffer.find('\n');
|
f = HTTPbuffer.find('\n');
|
||||||
if (f == std::string::npos) return false;
|
if (f == std::string::npos) return false;
|
||||||
tmpA = HTTPbuffer.substr(0, f);
|
tmpA = HTTPbuffer.substr(0, f);
|
||||||
while (tmpA.find('\r') != std::string::npos) {
|
while (tmpA.find('\r') != std::string::npos){tmpA.erase(tmpA.find('\r'));}
|
||||||
tmpA.erase(tmpA.find('\r'));
|
|
||||||
}
|
|
||||||
unsigned int chunkLen = 0;
|
unsigned int chunkLen = 0;
|
||||||
if (!tmpA.empty()){
|
if (!tmpA.empty()){
|
||||||
for (unsigned int i = 0; i < tmpA.size(); ++i){
|
for (unsigned int i = 0; i < tmpA.size(); ++i){
|
||||||
|
@ -846,9 +878,7 @@ void HTTP::parseVars(const std::string & data, std::map<std::string, std::string
|
||||||
size_t pos = 0;
|
size_t pos = 0;
|
||||||
while (pos < data.length()){
|
while (pos < data.length()){
|
||||||
size_t nextpos = data.find('&', pos);
|
size_t nextpos = data.find('&', pos);
|
||||||
if (nextpos == std::string::npos) {
|
if (nextpos == std::string::npos){nextpos = data.length();}
|
||||||
nextpos = data.length();
|
|
||||||
}
|
|
||||||
size_t eq_pos = data.find('=', pos);
|
size_t eq_pos = data.find('=', pos);
|
||||||
if (eq_pos < nextpos){
|
if (eq_pos < nextpos){
|
||||||
// there is a key and value
|
// there is a key and value
|
||||||
|
@ -896,9 +926,7 @@ void HTTP::Parser::Chunkify(const char * data, unsigned int size, Socket::Connec
|
||||||
}
|
}
|
||||||
if (sendingChunks){
|
if (sendingChunks){
|
||||||
// prepend the chunk size and \r\n
|
// prepend the chunk size and \r\n
|
||||||
if (!size){
|
if (!size){conn.SendNow("0\r\n\r\n", 5);}
|
||||||
conn.SendNow("0\r\n\r\n", 5);
|
|
||||||
}
|
|
||||||
size_t offset = 8;
|
size_t offset = 8;
|
||||||
unsigned int t_size = size;
|
unsigned int t_size = size;
|
||||||
char len[] = "\000\000\000\000\000\000\0000\r\n";
|
char len[] = "\000\000\000\000\000\000\0000\r\n";
|
||||||
|
|
|
@ -2,11 +2,11 @@
|
||||||
/// 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{
|
||||||
|
@ -15,7 +15,6 @@ namespace HTTP {
|
||||||
/// Reads variables from data, decodes and stores them to storage.
|
/// Reads variables from data, decodes and stores them to storage.
|
||||||
void parseVars(const std::string &data, std::map<std::string, std::string> &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:
|
||||||
|
@ -37,16 +36,18 @@ namespace HTTP {
|
||||||
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,
|
||||||
|
Socket::Connection &conn, bool bufferAllChunks = false);
|
||||||
void StartResponse(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 std::string &bodypart, Socket::Connection &conn);
|
||||||
void Chunkify(const char *data, unsigned int size, Socket::Connection &conn);
|
void Chunkify(const char *data, unsigned int size, Socket::Connection &conn);
|
||||||
void Proxy(Socket::Connection &from, Socket::Connection &to);
|
void Proxy(Socket::Connection &from, Socket::Connection &to);
|
||||||
void Clean();
|
void Clean();
|
||||||
void CleanPreserveHeaders();
|
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 body;
|
||||||
std::string method;
|
std::string method;
|
||||||
std::string url;
|
std::string url;
|
||||||
|
@ -58,6 +59,7 @@ namespace HTTP {
|
||||||
bool sendingChunks;
|
bool sendingChunks;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
std::string cnonce;
|
||||||
bool seenHeaders;
|
bool seenHeaders;
|
||||||
bool seenReq;
|
bool seenReq;
|
||||||
bool getChunks;
|
bool getChunks;
|
||||||
|
@ -78,6 +80,7 @@ namespace HTTP {
|
||||||
uint32_t getDefaultPort() const;
|
uint32_t getDefaultPort() const;
|
||||||
std::string getUrl() const;
|
std::string getUrl() const;
|
||||||
std::string getBareUrl() const;
|
std::string getBareUrl() const;
|
||||||
|
std::string getProxyUrl() const;
|
||||||
std::string host; ///< Hostname or IP address of URL
|
std::string host; ///< Hostname or IP address of URL
|
||||||
std::string protocol; ///< Protocol of URL
|
std::string protocol; ///< Protocol of URL
|
||||||
std::string port; ///< Port of URL
|
std::string port; ///< Port of URL
|
||||||
|
@ -90,4 +93,5 @@ namespace HTTP {
|
||||||
bool IPv6Addr;
|
bool IPv6Addr;
|
||||||
};
|
};
|
||||||
|
|
||||||
}//HTTP namespace
|
}// namespace HTTP
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue