1 // Sun, 18 Jun 2000 2 // Y.Takagi 3 4 #ifndef __HttpURLConnection_H 5 #define __HttpURLConnection_H 6 7 #include <iostream> 8 9 #include <list> 10 #include <string> 11 12 #include <Url.h> 13 14 using namespace std; 15 16 class Socket; 17 18 enum HTTP_RESPONSECODE { 19 HTTP_UNKNOWN = -1, // 20 21 HTTP_CONTINUE = 100, // Everything OK, keep going... 22 HTTP_SWITCH_PROC = 101, // Switching Protocols 23 24 HTTP_OK = 200, // OPTIONS/GET/HEAD/POST/TRACE command was successful 25 HTTP_CREATED, // PUT command was successful 26 HTTP_ACCEPTED, // DELETE command was successful 27 HTTP_NOT_AUTHORITATIVE, // Information isn't authoritative 28 HTTP_NO_CONTENT, // Successful command, no new data 29 HTTP_RESET, // Content was reset/recreated 30 HTTP_PARTIAL, // Only a partial file was recieved/sent 31 32 HTTP_MULTI_CHOICE = 300, // Multiple files match request 33 HTTP_MOVED_PERM, // Document has moved permanently 34 HTTP_MOVED_TEMP, // Document has moved temporarily 35 HTTP_SEE_OTHER, // See this other link... 36 HTTP_NOT_MODIFIED, // File not modified 37 HTTP_USE_PROXY, // Must use a proxy to access this URI 38 39 HTTP_BAD_REQUEST = 400, // Bad request 40 HTTP_UNAUTHORIZED, // Unauthorized to access host 41 HTTP_PAYMENT_REQUIRED, // Payment required 42 HTTP_FORBIDDEN, // Forbidden to access this URI 43 HTTP_NOT_FOUND, // URI was not found 44 HTTP_BAD_METHOD, // Method is not allowed 45 HTTP_NOT_ACCEPTABLE, // Not Acceptable 46 HTTP_PROXY_AUTH, // Proxy Authentication is Required 47 HTTP_REQUEST_TIMEOUT, // Request timed out 48 HTTP_CONFLICT, // Request is self-conflicting 49 HTTP_GONE, // Server has gone away 50 HTTP_LENGTH_REQUIRED, // A content length or encoding is required 51 HTTP_PRECON_FAILED, // Precondition failed 52 HTTP_ENTITY_TOO_LARGE, // URI too long 53 HTTP_REQ_TOO_LONG, // Request entity too large 54 HTTP_UNSUPPORTED_TYPE, // The requested media type is unsupported 55 56 HTTP_SERVER_ERROR = 500, // Internal server error 57 HTTP_INTERNAL_ERROR, // Feature not implemented 58 HTTP_BAD_GATEWAY, // Bad gateway 59 HTTP_UNAVAILABLE, // Service is unavailable 60 HTTP_GATEWAY_TIMEOUT, // Gateway connection timed out 61 HTTP_VERSION // HTTP version not supported 62 }; 63 64 struct Field { 65 string key; 66 string value; 67 Field() {} 68 Field(char *field); 69 Field(const char *k, const char *v); 70 Field(const Field &); 71 Field &operator = (const Field &); 72 bool operator == (const Field &); 73 }; 74 75 typedef list<Field> Fields; 76 77 class HttpURLConnection { 78 public: 79 HttpURLConnection(const BUrl &url); 80 virtual ~HttpURLConnection(); 81 82 virtual void connect(); 83 void disconnect(); 84 85 void setRequestMethod(const char *method); 86 const char *getRequestMethod() const; 87 void setRequestProperty(const char *key, const char *value); 88 89 const char *getContentType(); 90 const char *getContentEncoding(); 91 int getContentLength(); 92 long getDate(); 93 const char *getHeaderField(int n); 94 const char *getHeaderField(const char *); 95 const BUrl &getURL() const; 96 HTTP_RESPONSECODE getResponseCode(); 97 const char *getResponseMessage(); 98 99 bool getDoInput() const; 100 bool getDoOutput() const; 101 void setDoInput(bool doInput); 102 void setDoOutput(bool doOutput); 103 104 istream &getInputStream(); 105 ostream &getOutputStream(); 106 107 const char *getLastError() const; 108 void setLastError(const char *); 109 110 protected: 111 bool connected; 112 bool doInput; 113 bool doOutput; 114 BUrl url; 115 116 virtual void action(); 117 virtual void setRequest(); 118 virtual void setContent(); 119 virtual void getResponse(); 120 virtual void getContent(); 121 122 private: 123 Fields *__request; 124 Fields *__response; 125 Socket *__sock; 126 string __method; 127 string __response_message; 128 HTTP_RESPONSECODE __response_code; 129 string __error_msg; 130 }; 131 132 inline const char *HttpURLConnection::getLastError() const 133 { 134 return __error_msg.c_str(); 135 } 136 137 inline void HttpURLConnection::setLastError(const char *e) 138 { 139 __error_msg = e; 140 } 141 142 #endif // __HttpURLConnection_H 143