00001
00002
00003
00004
00005
00006
00007
00008
00009 #ifndef __libutilxx__sella__server__HTTPConnection_H__
00010 #define __libutilxx__sella__server__HTTPConnection_H__
00011
00012 #include "../../common.h"
00013 #include "../net/SocketException.h"
00014 #include "../net/TCPSocket.h"
00015 #include "Code.h"
00016 #include "HTTPRequest.h"
00017 #include "ServerException.h"
00018
00019 #include <set>
00020 #include <memory>
00021
00022 namespace sella {
00023 namespace server {
00024 class HTTPConnection;
00025 class HTTP;
00026 }
00027 }
00028
00029 class sella::server::HTTPConnection {
00030 public:
00031 typedef std::shared_ptr<HTTPConnection> shared;
00032
00033 static const timeval ReceiveTimeoutDefault;
00034 static const std::string MIMEFallback;
00035 static const std::string ContentTypeDefault;
00036
00037 public:
00038 HTTPConnection(const HTTP &http, sella::net::TCPSocket::shared &socket, timeval tv = ReceiveTimeoutDefault) throw ();
00039 virtual ~HTTPConnection() throw ();
00040
00041 static const HTTPConnection::shared shared_ptr(const HTTP &http, sella::net::TCPSocket::shared &socket, timeval tv = ReceiveTimeoutDefault) throw ();
00042
00043 const HTTPRequest& getRequest(void) throw (ServerException);
00044 bool isUsable(void) throw ();
00045
00046 void response(const std::string &body, const std::string &contentType = ContentTypeDefault, bool close = true) throw (ServerException);
00047 void response(const std::vector<char> &content, const std::string &contentType, bool close = true) throw (ServerException);
00048 void error(int code, const std::string &detail = std::string()) throw (ServerException);
00049 void error(int code, const std::vector<char> &content, const std::string &contentType) throw (ServerException);
00050
00051 void methodOptions(void) throw (ServerException);
00052 void methodGet(void) throw (ServerException);
00053 void methodHead(void) throw (ServerException);
00054 void methodPost(void) throw (ServerException);
00055 void methodPut(void) throw (ServerException);
00056 void methodDelete(void) throw (ServerException);
00057 void methodTrace(void) throw (ServerException);
00058 void methodConnect(void) throw (ServerException);
00059 void methodPatch(void) throw (ServerException);
00060
00061 sella::net::IPAddressPort getLocalIPAddressPort(void) const throw (ServerException);
00062 sella::net::IPAddressPort getRemoteIPAddressPort(void) const throw (ServerException);
00063
00064 void clear(void) throw ();
00065
00066 protected:
00067 const HTTP &http;
00068 sella::net::TCPSocket::shared socket;
00069 HTTPRequest request;
00070
00071 void response(int code, const std::string &body, const std::string &contentType = ContentTypeDefault, bool close = true) throw (ServerException);
00072 void response(int code, const std::vector<char> &content, const std::string &contentType, bool close = true) throw (ServerException);
00073
00074 void sendStatusLine(int code, const std::string &detail = std::string()) throw (sella::net::SocketException);
00075 void sendHeaders(size_t size, const std::string &contentType = ContentTypeDefault, bool close = true) throw (sella::net::SocketException);
00076 void sendBody(const std::string &body) throw (sella::net::SocketException);
00077 void sendContent(const std::vector<char> &content) throw (sella::net::SocketException);
00078
00079 void read(std::string &buffer) throw (ServerException);
00080 void chunker(void) throw (ServerException);
00081
00082 bool isDirectory(const std::string &path) const throw ();
00083 bool isFile(const std::string &file) const throw ();
00084
00085 const std::string buildHeaders(size_t contentSize, const std::string &contentType = ContentTypeDefault, bool close = true, bool useAllow = false, const std::string &transferEncoding = std::string()) throw ();
00086 std::string statusCodeString(int code) throw ();
00087
00088 std::string getMIMEType(const std::string &file, const std::string &fallback = std::string()) const throw (ServerException);
00089 std::string getFullpath(const std::string &file = std::string()) throw (ServerException);
00090 void getFileContent(const std::string &file, std::vector<char> &content, std::string &contentType) throw (ServerException);
00091 size_t getFileSize(std::ifstream &ifile) throw (ServerException);
00092 std::string getErrorBody(int code, const std::string &detail = std::string()) throw ();
00093 };
00094
00095 #endif
00096
00097
00098
00099