00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036 #ifndef INCL_ABSTRACT_HTTP_CONNECTION
00037 #define INCL_ABSTRACT_HTTP_CONNECTION
00038
00040 #include "base/globalsdef.h"
00041 #include "base/fscapi.h"
00042 #include "base/constants.h"
00043 #include "http/constants.h"
00044 #include "http/HttpAuthentication.h"
00045 #include "http/Proxy.h"
00046 #include "base/Log.h"
00047 #include "base/util/StringBuffer.h"
00048 #include "base/util/StringMap.h"
00049 #include "inputStream/InputStream.h"
00050 #include "inputStream/OutputStream.h"
00051
00052 BEGIN_FUNAMBOL_NAMESPACE
00053
00054
00055
00056 #define HTTP_HEADER_USER_AGENT "User-Agent"
00057 #define HTTP_HEADER_ACCEPT "Accept"
00058 #define HTTP_HEADER_ACCEPT_ENCODING "Accept-Encoding"
00059 #define HTTP_HEADER_CONTENT_TYPE "Content-Type"
00060 #define HTTP_HEADER_CONTENT_LENGTH "Content-Length"
00061 #define HTTP_HEADER_UNCOMPRESSED_CONTENT_LENGTH "Uncompressed-Content-Length"
00062 #define HTTP_HEADER_SET_COOKIE "Set-Cookie"
00063 #define HTTP_HEADER_COOKIE "Cookie"
00064 #define HTTP_HEADER_AUTHORIZATION "Authorization"
00065 #define HTTP_HEADER_CONTENT_ENCODING "Content-Encoding"
00066 #define HTTP_HEADER_TRANSFER_ENCODING "Transfer-Encoding"
00067 #define HTTP_HEADER_CONTENT_RANGE "Content-Range"
00068
00069
00070
00071 #define HTTP_HEADER_X_FUNAMBOL_DEVICE_ID "x-funambol-syncdeviceid"
00072 #define HTTP_HEADER_X_FUNAMBOL_FILE_SIZE "x-funambol-file-size"
00073 #define HTTP_HEADER_X_FUNAMBOL_LUID "x-funambol-luid"
00074
00075
00076
00077 #define DEFAULT_REQUEST_MAX_CHUNK_SIZE 50000
00078
00079
00080 #define DEFAULT_HTTP_TIMEOUT 300
00081
00082
00099 class AbstractHttpConnection {
00100
00101 public:
00102
00104 enum RequestMethod {
00105 MethodGet,
00106 MethodPost,
00107 MethodPut,
00108 MethodHead
00109 };
00110
00111 enum ConnectionStatus {
00112 StatusNoError = 0,
00113 StatusCancelledByUser = -1,
00114 StatusInternalError = -2,
00115 StatusReadingError = -3,
00116 StatusWritingError = -4,
00117 StatusInvalidParam = -5,
00118 StatusNetworkError = -6
00119 };
00120
00122 AbstractHttpConnection(const char* ua) : userAgent(ua) {
00123 method = MethodPost;
00124 auth = NULL;
00125 chunkSize = DEFAULT_REQUEST_MAX_CHUNK_SIZE;
00126 SSLVerifyServer = true;
00127 SSLVerifyHost = true;
00128 compression_enabled = false;
00129 keepalive = false;
00130 timeout = DEFAULT_HTTP_TIMEOUT;
00131 }
00132
00133 virtual ~AbstractHttpConnection() {}
00134
00135
00141 virtual int open(const URL& url, RequestMethod method = MethodPost) = 0;
00142
00148 virtual int close() = 0;
00149
00154 void setRequestMethod(RequestMethod method) {
00155 this->method = method;
00156 }
00157
00158
00169 virtual int request(InputStream& data, OutputStream& response) = 0;
00170
00171
00183 virtual void setRequestHeader(const char* key, const char* value) {
00184 requestHeaders.put(key, value);
00185 }
00186
00187
00194 virtual StringBuffer getResponseHeader(const char* key) {
00195 return responseHeaders[key];
00196 }
00197
00203 virtual void setAuthentication(HttpAuthentication *auth) {
00204 this->auth = auth;
00205 }
00206
00207
00209 void setChunkSize(const int size) { chunkSize = size; }
00210 int getChunkSize() { return chunkSize; }
00211
00218 virtual void setURL(const URL& newURL) { url = newURL; }
00219
00223 virtual const URL& getURL() const { return url; }
00224
00230 virtual void setTimeout(unsigned int t) { timeout = t; }
00231
00235 virtual unsigned int getTimeout() const { return timeout; }
00236
00244 virtual bool getSSLVerifyServer() const { return SSLVerifyServer; }
00245 virtual void setSSLVerifyServer(bool value) { SSLVerifyServer = value; }
00246
00254 virtual bool getSSLVerifyHost() const { return SSLVerifyHost; }
00255 virtual void setSSLVerifyHost(bool value) { SSLVerifyHost = value; }
00256
00260 virtual bool getCompression() const { return compression_enabled; }
00261 virtual void setCompression(bool value) { compression_enabled = value; }
00262
00267 virtual void setKeepAlive(bool val) { keepalive = val; }
00268
00269
00270 protected:
00271
00272 URL url;
00273
00274 RequestMethod method;
00275
00277 StringMap requestHeaders;
00278
00280 StringMap responseHeaders;
00281
00283 HttpAuthentication* auth;
00284
00286 int chunkSize;
00287
00288 Proxy proxy;
00289
00290 unsigned int timeout;
00291
00292 StringBuffer userAgent;
00293
00294
00295 StringBuffer SSLServerCertificates;
00296 bool SSLVerifyServer;
00297 bool SSLVerifyHost;
00298 bool compression_enabled;
00299
00302 bool keepalive;
00303
00304
00305 public:
00306
00307
00314 StringBuffer parseJSessionId(const StringBuffer& input) {
00315
00316 StringBuffer ret;
00317 if (input.empty()) {
00318 return ret;
00319 }
00320
00321
00322
00323 bool found = false;
00324 StringBuffer attribute;
00325 ArrayList attributes;
00326 input.split(attributes, ";");
00327 for (int i=0; i<attributes.size(); i++) {
00328 attribute = *(StringBuffer*)attributes[i];
00329 attribute.trim();
00330 if (attribute.ifind("JSESSIONID") != StringBuffer::npos) {
00331 found = true;
00332 break;
00333 }
00334 }
00335
00336 if (found) {
00337 ArrayList tokens;
00338 attribute.split(tokens, "=");
00339
00340 StringBuffer* prop = (StringBuffer*)tokens.get(0);
00341 if (prop && prop->icmp("JSESSIONID")) {
00342 StringBuffer* val = (StringBuffer*)tokens.get(1);
00343 if (val && !val->empty()) {
00344 ret = *val;
00345 }
00346 }
00347 }
00348 return ret;
00349 }
00350
00351 };
00352
00353 END_FUNAMBOL_NAMESPACE
00354
00356 #endif
00357