mirror of https://github.com/PurpleI2P/i2pd.git
I2P: End-to-End encrypted and anonymous Internet
https://i2pd.website/
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
164 lines
4.8 KiB
164 lines
4.8 KiB
9 years ago
|
/*
|
||
|
* Copyright (c) 2013-2016, The PurpleI2P Project
|
||
|
*
|
||
|
* This file is part of Purple i2pd project and licensed under BSD3
|
||
|
*
|
||
|
* See full license text in LICENSE file at top of project tree
|
||
|
*/
|
||
|
|
||
|
#ifndef HTTP_H__
|
||
|
#define HTTP_H__
|
||
|
|
||
|
#include <cstring>
|
||
|
#include <map>
|
||
8 years ago
|
#include <list>
|
||
9 years ago
|
#include <sstream>
|
||
|
#include <string>
|
||
|
#include <vector>
|
||
|
|
||
|
namespace i2p {
|
||
|
namespace http {
|
||
|
const char CRLF[] = "\r\n"; /**< HTTP line terminator */
|
||
|
const char HTTP_EOH[] = "\r\n\r\n"; /**< HTTP end-of-headers mark */
|
||
9 years ago
|
extern const std::vector<std::string> HTTP_METHODS; /**< list of valid HTTP methods */
|
||
|
extern const std::vector<std::string> HTTP_VERSIONS; /**< list of valid HTTP versions */
|
||
9 years ago
|
|
||
|
struct URL {
|
||
|
std::string schema;
|
||
|
std::string user;
|
||
|
std::string pass;
|
||
|
std::string host;
|
||
|
unsigned short int port;
|
||
|
std::string path;
|
||
|
std::string query;
|
||
|
std::string frag;
|
||
|
|
||
|
URL(): schema(""), user(""), pass(""), host(""), port(0), path(""), query(""), frag("") {};
|
||
|
|
||
|
/**
|
||
|
* @brief Tries to parse url from string
|
||
|
* @return true on success, false on invalid url
|
||
|
*/
|
||
9 years ago
|
bool parse (const char *str, std::size_t len = 0);
|
||
9 years ago
|
bool parse (const std::string& url);
|
||
|
|
||
|
/**
|
||
|
* @brief Parse query part of url to key/value map
|
||
|
* @note Honestly, this should be implemented with std::multimap
|
||
|
*/
|
||
|
bool parse_query(std::map<std::string, std::string> & params);
|
||
|
|
||
|
/**
|
||
|
* @brief Serialize URL structure to url
|
||
|
* @note Returns relative url if schema if empty, absolute url otherwise
|
||
|
*/
|
||
|
std::string to_string ();
|
||
|
};
|
||
|
|
||
8 years ago
|
struct HTTPMsg
|
||
|
{
|
||
9 years ago
|
std::map<std::string, std::string> headers;
|
||
9 years ago
|
|
||
9 years ago
|
void add_header(const char *name, std::string & value, bool replace = false);
|
||
9 years ago
|
void add_header(const char *name, const char *value, bool replace = false);
|
||
|
void del_header(const char *name);
|
||
9 years ago
|
|
||
|
/** @brief Returns declared message length or -1 if unknown */
|
||
8 years ago
|
long int content_length() const;
|
||
9 years ago
|
};
|
||
|
|
||
8 years ago
|
struct HTTPReq
|
||
|
{
|
||
|
std::list<std::pair<std::string, std::string> > headers;
|
||
9 years ago
|
std::string version;
|
||
|
std::string method;
|
||
|
std::string uri;
|
||
|
|
||
|
HTTPReq (): version("HTTP/1.0"), method("GET"), uri("/") {};
|
||
|
|
||
|
/**
|
||
|
* @brief Tries to parse HTTP request from string
|
||
|
* @return -1 on error, 0 on incomplete query, >0 on success
|
||
|
* @note Positive return value is a size of header
|
||
|
*/
|
||
|
int parse(const char *buf, size_t len);
|
||
|
int parse(const std::string& buf);
|
||
|
|
||
|
/** @brief Serialize HTTP request to string */
|
||
|
std::string to_string();
|
||
8 years ago
|
void write(std::ostream & o);
|
||
8 years ago
|
|
||
8 years ago
|
void AddHeader (const std::string& name, const std::string& value);
|
||
|
void UpdateHeader (const std::string& name, const std::string& value);
|
||
|
void RemoveHeader (const std::string& name);
|
||
|
std::string GetHeader (const std::string& name) const;
|
||
9 years ago
|
};
|
||
|
|
||
9 years ago
|
struct HTTPRes : HTTPMsg {
|
||
9 years ago
|
std::string version;
|
||
|
std::string status;
|
||
|
unsigned short int code;
|
||
9 years ago
|
/**
|
||
|
* @brief Simplifies response generation
|
||
|
*
|
||
|
* If this variable is set, on @a to_string() call:
|
||
|
* * Content-Length header will be added if missing,
|
||
|
* * contents of @a body will be included in generated response
|
||
9 years ago
|
*/
|
||
|
std::string body;
|
||
9 years ago
|
|
||
|
HTTPRes (): version("HTTP/1.1"), status("OK"), code(200) {}
|
||
|
|
||
|
/**
|
||
|
* @brief Tries to parse HTTP response from string
|
||
|
* @return -1 on error, 0 on incomplete query, >0 on success
|
||
|
* @note Positive return value is a size of header
|
||
|
*/
|
||
|
int parse(const char *buf, size_t len);
|
||
|
int parse(const std::string& buf);
|
||
|
|
||
9 years ago
|
/**
|
||
|
* @brief Serialize HTTP response to string
|
||
9 years ago
|
* @note If @a version is set to HTTP/1.1, and Date header is missing,
|
||
9 years ago
|
* it will be generated based on current time and added to headers
|
||
9 years ago
|
* @note If @a body is set and Content-Length header is missing,
|
||
9 years ago
|
* this header will be added, based on body's length
|
||
|
*/
|
||
9 years ago
|
std::string to_string();
|
||
|
|
||
8 years ago
|
void write(std::ostream & o);
|
||
|
|
||
9 years ago
|
/** @brief Checks that response declared as chunked data */
|
||
8 years ago
|
bool is_chunked() const ;
|
||
9 years ago
|
|
||
|
/** @brief Checks that response contains compressed data */
|
||
8 years ago
|
bool is_gzipped(bool includingI2PGzip = true) const;
|
||
9 years ago
|
};
|
||
|
|
||
|
/**
|
||
|
* @brief returns HTTP status string by integer code
|
||
|
* @param code HTTP code [100, 599]
|
||
|
* @return Immutable string with status
|
||
|
*/
|
||
|
const char * HTTPCodeToStatus(int code);
|
||
|
|
||
|
/**
|
||
|
* @brief Replaces %-encoded characters in string with their values
|
||
|
* @param data Source string
|
||
|
* @param null If set to true - decode also %00 sequence, otherwise - skip
|
||
|
* @return Decoded string
|
||
|
*/
|
||
|
std::string UrlDecode(const std::string& data, bool null = false);
|
||
9 years ago
|
|
||
|
/**
|
||
|
* @brief Merge HTTP response content with Transfer-Encoding: chunked
|
||
|
* @param in Input stream
|
||
|
* @param out Output stream
|
||
|
* @return true on success, false otherwise
|
||
|
*/
|
||
|
bool MergeChunkedResponse (std::istream& in, std::ostream& out);
|
||
9 years ago
|
} // http
|
||
|
} // i2p
|
||
|
|
||
|
#endif /* HTTP_H__ */
|