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