2015-09-06 14:15:46 +00:00
|
|
|
#ifndef _HTTP_H__
|
|
|
|
#define _HTTP_H__
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
#include <map>
|
2015-09-17 09:47:16 +00:00
|
|
|
#include <sstream>
|
2015-09-06 14:15:46 +00:00
|
|
|
|
|
|
|
namespace i2p {
|
|
|
|
namespace util {
|
|
|
|
namespace http {
|
|
|
|
|
|
|
|
class Request {
|
2015-09-06 16:51:19 +00:00
|
|
|
|
2015-09-06 14:15:46 +00:00
|
|
|
void parseRequestLine(const std::string& line);
|
2015-09-06 16:51:19 +00:00
|
|
|
|
2015-09-06 14:15:46 +00:00
|
|
|
void parseHeaderLine(const std::string& line);
|
2015-09-17 09:47:16 +00:00
|
|
|
|
|
|
|
void parseHeader(std::stringstream& ss);
|
|
|
|
|
|
|
|
void setIsComplete();
|
2015-09-06 14:15:46 +00:00
|
|
|
public:
|
2015-09-06 18:34:50 +00:00
|
|
|
Request() = default;
|
2015-09-06 14:15:46 +00:00
|
|
|
|
2015-09-06 18:34:50 +00:00
|
|
|
Request(const std::string& data);
|
2015-09-06 16:51:19 +00:00
|
|
|
|
2015-09-06 14:15:46 +00:00
|
|
|
std::string getMethod() const;
|
|
|
|
|
|
|
|
std::string getUri() const;
|
|
|
|
|
|
|
|
std::string getHost() const;
|
|
|
|
|
|
|
|
int getPort() const;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @throw std::out_of_range if no such header exists
|
|
|
|
*/
|
|
|
|
std::string getHeader(const std::string& name) const;
|
|
|
|
|
2015-09-07 10:31:57 +00:00
|
|
|
std::string getContent() const;
|
|
|
|
|
2015-09-17 09:47:16 +00:00
|
|
|
bool hasData() const;
|
|
|
|
|
|
|
|
bool isComplete() const;
|
|
|
|
|
|
|
|
void clear();
|
|
|
|
|
|
|
|
void update(const std::string& data);
|
|
|
|
|
2015-09-06 14:15:46 +00:00
|
|
|
private:
|
2015-09-17 09:47:16 +00:00
|
|
|
std::string header_part;
|
|
|
|
|
2015-09-06 14:15:46 +00:00
|
|
|
std::string method;
|
|
|
|
std::string uri;
|
|
|
|
std::string host;
|
2015-09-07 10:31:57 +00:00
|
|
|
std::string content;
|
2015-09-06 14:15:46 +00:00
|
|
|
int port;
|
|
|
|
std::map<std::string, std::string> headers;
|
2015-09-17 09:47:16 +00:00
|
|
|
bool has_data;
|
|
|
|
bool has_header;
|
|
|
|
bool is_complete;
|
2015-09-06 14:15:46 +00:00
|
|
|
};
|
|
|
|
|
2015-09-06 16:51:19 +00:00
|
|
|
class Response {
|
|
|
|
public:
|
2015-09-06 18:34:50 +00:00
|
|
|
Response() = default;
|
2015-09-06 16:51:19 +00:00
|
|
|
|
2015-09-06 18:34:50 +00:00
|
|
|
Response(int status, const std::string& content = "");
|
2015-09-06 16:51:19 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @note overrides existing header values with the same name
|
|
|
|
*/
|
|
|
|
void setHeader(const std::string& name, const std::string& value);
|
|
|
|
|
|
|
|
std::string toString() const;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return the message associated with the satus of this response, or the
|
|
|
|
* empty string if the status number is invalid
|
|
|
|
*/
|
|
|
|
std::string getStatusMessage() const;
|
|
|
|
|
2015-09-06 18:34:50 +00:00
|
|
|
void setContentLength();
|
|
|
|
|
2015-09-06 16:51:19 +00:00
|
|
|
private:
|
|
|
|
int status;
|
2015-09-06 18:34:50 +00:00
|
|
|
std::string content;
|
2015-09-06 16:51:19 +00:00
|
|
|
std::map<std::string, std::string> headers;
|
|
|
|
};
|
|
|
|
|
2015-09-17 09:47:16 +00:00
|
|
|
/**
|
|
|
|
* Handle server side includes.
|
|
|
|
*/
|
|
|
|
std::string preprocessContent(const std::string& content, const std::string& path);
|
|
|
|
|
2015-09-17 09:59:04 +00:00
|
|
|
/**
|
|
|
|
* @return the MIME type based on the extension of the given filename
|
|
|
|
*/
|
|
|
|
std::string getMimeType(const std::string& filename);
|
|
|
|
|
2015-09-06 14:15:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // _HTTP_H__
|