merge with master
[framework/osp/net.git] / src / inc / FNetHttp_HttpCommon.h
1 //
2 // Open Service Platform
3 // Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 //     http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17
18 /**
19  * @file                FNetHttp_HttpCommon.h
20  * @brief               This is the header file for defining common types and utility functions for %HTTP operations.
21  *
22  * This header file contains the definition of common types and utility functions for %HTTP operations.
23  */
24
25 #ifndef _FNET_HTTP_INTERNAL_HTTP_COMMON_H_
26 #define _FNET_HTTP_INTERNAL_HTTP_COMMON_H_
27
28 #include <curl.h>
29 #include <FNetHttpHttpTypes.h>
30 #include <FBaseSysLog.h>
31
32 namespace Tizen { namespace Base
33 {
34 class ByteBuffer;
35 class String;
36 } }
37
38 namespace Tizen { namespace Base { namespace Collection
39 {
40 class IList;
41 } } }
42
43 namespace Tizen { namespace Base { namespace Runtime
44 {
45 class Mutex;
46 } } }
47
48 namespace Tizen { namespace Net { namespace Http
49 {
50 class _HttpSslInfo;
51 class _HttpTransactionEvent;
52
53 /**
54  * The User-Agent header name
55  */
56 _OSP_EXPORT_ extern const wchar_t _HTTP_USERAGENT_HEADER_NAME[];
57
58 /**
59  * The Content-Length header name
60  */
61 _OSP_EXPORT_ extern const wchar_t _HTTP_CONTENT_LENGTH_HEADER_NAME[];
62
63 /**
64  * The Content-Type header name
65  */
66 _OSP_EXPORT_ extern const wchar_t _HTTP_CONTENT_TYPE_HEADER_NAME[];
67
68 /**
69  * The Transfer-Encoding header name
70  */
71 _OSP_EXPORT_ extern const wchar_t _HTTP_TRANSFER_ENCODING_HEADER_NAME[];
72
73 /**
74  * The Expect header name
75  */
76 _OSP_EXPORT_ extern const wchar_t _HTTP_EXPECT_HEADER_NAME[];
77
78 /**
79  * The Set-Cookie name
80  */
81 _OSP_EXPORT_ extern const wchar_t _HTTP_SET_COOKIE_HEADER_NAME[];
82
83 /**
84  * The Proxy-Authenticate name
85  */
86 _OSP_EXPORT_ extern const wchar_t _HTTP_PROXY_AUTHENTICATE_HEADER_NAME[];
87
88 /**
89  * The WWW-Authenticate
90  */
91 _OSP_EXPORT_ extern const wchar_t _HTTP_WWW_AUTHENTICATE_HEADER_NAME[];
92
93 /**
94  * The chunked header value
95  */
96 _OSP_EXPORT_ extern const wchar_t _HTTP_CHUNKED_HEADER_VALUE[];
97
98 /**
99  * The protocol scheme of http://
100  */
101 _OSP_EXPORT_ extern const wchar_t _HTTP_PROTOCOL_SCHEME[];
102
103 /**
104  * The protocol scheme of https://
105  */
106 _OSP_EXPORT_ extern const wchar_t _HTTPS_PROTOCOL_SCHEME[];
107
108 /**
109  * The default charset of body part.
110  */
111 _OSP_EXPORT_ extern const wchar_t _HTTP_DEFAULT_BODY_CHARSET[];
112
113 /**
114  * The prefix of multipart/form-data.
115  */
116 _OSP_EXPORT_ extern const wchar_t _MULTI_PART_FORM_DATA_PREFIX[];
117
118 /**
119  * The default charset of string part.
120  */
121 _OSP_EXPORT_ extern const wchar_t _HTTP_STRING_PART_DEFAULT_CHARSET[];
122
123 /**
124  * The default charset of file part.
125  */
126 _OSP_EXPORT_ extern const wchar_t _HTTP_FILE_PART_DEFAULT_CHARSET[];
127
128 /**
129  * The character set of boundary
130  */
131 _OSP_EXPORT_ extern const wchar_t _HTTP_MULTI_PART_BOUNDARY_CHARACTER_SET[];
132
133 /**
134  * The length of character set of boundary
135  */
136 static const int _HTTP_MULTI_PART_BOUNDARY_CHARACTER_SET_LENGTH = 62;
137
138 /**
139  * The delimiter of cookie value
140  */
141 _OSP_EXPORT_ extern const wchar_t _HTTP_SET_COOKIE_DELIMITER[];
142
143 /**
144  * The delimiter of cookie flag
145  */
146 _OSP_EXPORT_ extern const wchar_t _HTTP_SET_COOKIE_FLAG_DELIMITER[];
147
148 /**
149  * The Comment flag name of Cookie
150  */
151 _OSP_EXPORT_ extern const wchar_t _HTTP_COOKIE_COMMENT_NAME[];
152
153 /**
154  * The CommentURL flag name of Cookie
155  */
156 _OSP_EXPORT_ extern const wchar_t _HTTP_COOKIE_COMMENTURL_NAME[];
157
158 /**
159  * The Discard flag name of Cookie
160  */
161 _OSP_EXPORT_ extern const wchar_t _HTTP_COOKIE_DISCARD_NAME[];
162
163 /**
164  * The Expires flag name of Cookie
165  */
166 _OSP_EXPORT_ extern const wchar_t _HTTP_COOKIE_EXPIRES_NAME[];
167
168 /**
169  * The Domain flag name of Cookie
170  */
171 _OSP_EXPORT_ extern const wchar_t _HTTP_COOKIE_DOMAIN_NAME[];
172
173 /**
174  * The Max-Age flag name of Cookie
175  */
176 _OSP_EXPORT_ extern const wchar_t _HTTP_COOKIE_MAX_AGE_NAME[];
177
178 /**
179  * The Path flag name of Cookie
180  */
181 _OSP_EXPORT_ extern const wchar_t _HTTP_COOKIE_PATH_NAME[];
182
183 /**
184  * The Port flag name of Cookie
185  */
186 _OSP_EXPORT_ extern const wchar_t _HTTP_COOKIE_PORT_NAME[];
187
188 /**
189  * The Secure flag name of Cookie
190  */
191 _OSP_EXPORT_ extern const wchar_t _HTTP_COOKIE_SECURE_NAME[];
192
193 /**
194  * The Version flag name of Cookie
195  */
196 _OSP_EXPORT_ extern const wchar_t _HTTP_COOKIE_VERSION_NAME[];
197
198 /**
199  * The HttpOnly flag name of Cookie
200  */
201 _OSP_EXPORT_ extern const wchar_t _HTTP_COOKIE_HTTP_ONLY[];
202
203 /**
204  * The carriage return and line feed characters
205  */
206 _OSP_EXPORT_ extern const wchar_t _HTTP_CARRIAGE_RETURN_LINE_FEED[];
207
208 /**
209  * The #HttpOnly_ prefix of curl
210  */
211 _OSP_EXPORT_ extern const wchar_t _CURL_COOKIE_HTTP_ONLY_PREFIX[];
212
213 /**
214  * The path of cookie file ({application-home dir}/data/cookies.txt)
215  */
216 _OSP_EXPORT_ extern const wchar_t _HTTP_COOKIE_FILE[];
217
218 static const int _CURL_COOKIE_DOMAIN_NAME_INDEX = 0;
219
220 static const int _CURL_COOKIE_COOKIE_NAME_INDEX = 5;
221
222 static const int _CURL_COOKIE_COOKIE_VALUE_INDEX = 6;
223
224 static const int _COOKIE_PREFIX_LENGTH = 7;
225
226 static const long long _HTTP_INVALID_CONTENT_LENGTH = -1;
227
228 static const int _HTTP_INVALID_ID = -1;
229
230 /**
231  * The maximum number of transactions allowed (NET_HTTP_SESSION_MODE_NORMAL mode)
232  */
233 static const int _MAX_HTTP_TRANSACTIONS_PER_SESSION_NORMAL = 1;
234
235 /**
236  * The maximum number of transactions allowed (NET_HTTP_SESSION_MODE_PIPELINING mode)
237  */
238 static const int _MAX_HTTP_TRANSACTIONS_PER_SESSION_PIPE = 5;
239
240 /**
241  * The maximum number of transactions allowed (NET_HTTP_SESSION_MODE_PIPELINING mode), '0' means that the platform does not limit the number of maximum HttpTransaction that that HttpSession can have.
242  */
243 static const int _MAX_HTTP_TRANSACTIONS_PER_SESSION_MULTIPLE_HOST = 0;
244
245 /**
246  * The maximum number of session that application can have.
247  */
248 static const int _MAX_HTTP_SESSIONS_PER_APPID_PER_PROTOCOL = 10;
249
250 /**
251  * The length of multi-part boundary
252  */
253 static const int _HTTP_MULTIPART_BOUNDARY_LENGTH = 40;
254
255 /**
256  * The default connection timeout
257  */
258 static const int _HTTP_DEFAULT_CONNECTION_TIMEOUT = 30;
259
260 /**
261  * The http default port
262  */
263 static const int _HTTP_DEFAULT_PORT = 80;
264
265 /**
266  * The event type of transaction
267  */
268 enum _HttpTransactionEventType
269 {
270         _HTTP_TRANSACTION_EVENT_TYPE_UNDEFINED = 0,
271         _HTTP_TRANSACTION_EVENT_TYPE_HEADER_COMPLETED,
272         _HTTP_TRANSACTION_EVENT_TYPE_READY_TO_READ,
273         _HTTP_TRANSACTION_EVENT_TYPE_READY_TO_WRITE,
274         _HTTP_TRANSACTION_EVENT_TYPE_COMPLETD,
275         _HTTP_TRANSACTION_EVENT_TYPE_ABORTED,
276         _HTTP_TRANSACTION_EVENT_TYPE_CERT_VERIFICATION_REQUIRED,
277         _HTTP_TRANSACTION_EVENT_TYPE_CERT_VERIFICATION_REQUESTED,
278         _HTTP_TRANSACTION_EVENT_TYPE_UPLOAD_PROGRESS,
279         _HTTP_TRANSACTION_EVENT_TYPE_DOWNLOAD_PROGRESS
280 };
281
282 /**
283  * The maximum size of header
284  */
285 static const int _HTTP_DEFAULT_HEADER_SIZE = 1024;
286
287 /**
288  * The maximum limit of depth for certificate verification.
289  */
290 static const int _HTTP_CERT_VERIFICATION_DEPTH_LIMIT = 9;
291
292 /**
293  * The maximum size of certificate subject.
294  */
295 static const int _HTTP_CERT_SUBJECT_SIZE = 256;
296
297 /**
298  * The maximum size of body to read.
299  */
300 static const int _MAX_HTTP_BODY_SIZE = CURL_MAX_WRITE_SIZE;
301
302 /**
303  * The length of multi-part boundary
304  */
305 static const int _HTTP_NETWORK_ERROR = -1;
306
307 static const long _CURL_HTTP_AUTH_NONE = 0;                 //none
308 static const long _CURL_HTTP_AUTH_BASIC = 1;                    // The constant for basic authentication
309 static const long _CURL_HTTP_AUTH_DIGEST = 2;               // The constant for digest authentication
310 static const long _CURL_HTTP_AUTH_GSSNEGOTIATE = 4;         // The constant for gss-negotiate authentication
311 static const long _CURL_HTTP_AUTH_NTLM = 8;                  // The constant for ntlm authentication
312 static const long _CURL_HTTP_AUTH_DIGEST_IE = 16;            //Not Support
313
314 class _HttpUtility
315 {
316
317 public:
318         /**
319          * Checks whether the host address contains the protocol scheme such as "http://" or "https://".
320          *
321          * @return              @c true if the host address contains the protocol scheme, @n
322          *                              else @c false
323          * @param[in]   host    The host address
324          */
325         static bool HasProtocolScheme(const Tizen::Base::String& host);
326
327         /**
328          * Gets the protocol scheme.
329          *
330          * @return              The protocol scheme.
331          * @param[in]   host                            The host address
332          * @exception   E_SUCCESS                       The method is successful.
333          * @exception   E_INVALID_ARG       Invalid argument.
334          * @remarks             The specific error code can be accessed using the GetLastResult() method.
335          */
336         static Tizen::Base::String GetProtocolScheme(const Tizen::Base::String& host);
337
338         /**
339          * Compares the values of the two hosts.
340          *
341          * @return              @c true if the host part of @c host0 equal to the @c host1, @n
342          *                              else @c false
343          * @param[in]   host0                           The host address
344          * @param[in]   host1                           The host address
345          * @exception   E_SUCCESS                       The method is successful.
346          * @exception   E_INVALID_ARG       Invalid argument.
347          * @remarks             The specific error code can be accessed using the GetLastResult() method.
348          */
349         static bool CompareHost(const Tizen::Base::String& host0, const Tizen::Base::String& host1);
350
351         /**
352          * Gets the cookie file path.
353          *
354          * @return              The cookie file path.
355          */
356         static Tizen::Base::String GetCookieFilePath(void);
357
358         /**
359          * Converts NetHttpTransactionEventType to string name.
360          *
361          * @return              The string of NetHttpTransactionEventType
362          * @param[in]   type                            The type of NetHttpTransactionEventType
363          */
364         static const char* ConvertHttpTransactionEventTypeToString(_HttpTransactionEventType type);
365
366         /**
367          * Converts NetHttpCertificateVerificationFlag to string name.
368          *
369          * @return              The string of NetHttpCertificateVerificationFlag
370          * @param[in]   flag                            The flag of NetHttpCertificateVerificationFlag
371          */
372         static const char* ConvertNetHttpCertificateVerificationFlagToString(NetHttpCertificateVerificationFlag flag);
373
374         /**
375          * Adds a buffer to the header buffer.
376          *
377          * @return              An error code
378          * @param[in]   ppSrcBuffer                     The source buffer, it can be replaced with new buffer.
379          * @param[in]   destBuffer                      The destination buffer to be added
380          * @exception   E_OUT_OF_MEMORY         The memory is insufficient.
381          * @exception   E_SYSTEM                        A system error has occurred.
382          */
383         static result AddHeaderByteBufferToBuffer(Tizen::Base::ByteBuffer** ppSrcBuffer, const Tizen::Base::ByteBuffer& destBuffer);
384
385         /**
386          * Adds a bytes to the header buffer.
387          *
388          * @return              An error code
389          * @param[in]   ppSrcBuffer                     The source buffer, it can be replaced with new buffer.
390          * @param[in]   pDestBytes                      The destination bytes
391          * @param[in]   destBytesSize           The size of destination bytes
392          * @exception   E_OUT_OF_MEMORY         The memory is insufficient.
393          * @exception   E_SYSTEM                        A system error has occurred.
394          */
395         static result AddHeaderBytesToBuffer(Tizen::Base::ByteBuffer** ppSrcBuffer, const byte* pDestBytes, int destBytesSize);
396
397         /**
398          * Adds a destination buffer to the source buffer.
399          *
400          * @return              An error code
401          * @param[in]   ppSrcBuffer                     The source buffer, it can be replaced with new buffer.
402          * @param[in]   destBuffer                      The destination buffer to be added
403          * @param[in]   defaultExtendedSize     The extended size of buffer.
404          * @exception   E_OUT_OF_MEMORY         The memory is insufficient.
405          * @exception   E_SYSTEM                        A system error has occurred.
406          */
407         static result AddByteBufferToBuffer(Tizen::Base::ByteBuffer** ppSrcBuffer, const Tizen::Base::ByteBuffer& destBuffer, int defaultExtendedSize);
408
409         /**
410          * Adds a bytes to the buffer.
411          *
412          * @return              An error code
413          * @param[in]   ppSrcBuffer                     The source buffer, it can be replaced with new buffer.
414          * @param[in]   pDestBytes                      The destination bytes
415          * @param[in]   destBytesSize           The size of destination bytes
416          * @param[in]   defaultExtendedSize     The extended size of buffer.
417          * @exception   E_OUT_OF_MEMORY         The memory is insufficient.
418          * @exception   E_SYSTEM                        A system error has occurred.
419          */
420         static result AddBytesToBuffer(Tizen::Base::ByteBuffer** ppSrcBuffer, const byte* pDestBytes, int destBytesSize, int defaultExtendedSize);
421
422         /**
423          * Prints a curl error code.
424          *
425          * @param[in]   code                    The error code of CURLMcode
426          */
427         static void PrintCurlMultiErrorCode(CURLMcode code);
428
429         /**
430          * Destroy system variables for HTTP.
431          */
432         static void DestroyHttp(void);
433
434         /**
435          * Once block for thread-safe singleton.
436          */
437         static void     InitializeHttpOnce(void);
438
439         /**
440          * Initialize system variables for HTTP.
441          */
442         static void InitializeHttp(void);
443
444         /**
445          * Get the system mutex.
446          */
447         static Tizen::Base::Runtime::Mutex* GetHttpMutex(void);
448
449         /**
450          * Converts the auth scheme of curl to the auth scheme of osp.
451          *
452          * @return              The auth scheme of osp
453          * @param[in]   isProxyAuth                     @c true if the @c curlAuthScheme is proxy auth, @n
454          *                                                                      else @c false
455          * @param[in]   curlAuthScheme          The auth scheme of curl
456          */
457         static NetHttpAuthScheme GetHttpAuthScheme(bool isProxyAuth, long curlAuthScheme);
458
459         /**
460          * Gets the name of auth scheme.
461          *
462          * @return              The name of auth scheme
463          * @param[in]   httpAuthScheme          The auth scheme of osp
464          */
465         static const char* GetHttpAuthSchemeByString(NetHttpAuthScheme httpAuthScheme);
466
467         /**
468          * Converts the auth scheme of osp to the auth scheme of curl.
469          *
470          * @return              The auth scheme of curl
471          * @param[in]   httpAuthScheme          The auth scheme of osp
472          */
473         static long GetHttpCurlAuthScheme(NetHttpAuthScheme httpAuthScheme);
474
475         /**
476          * Converts a curl error code to an osp error.
477          *
478          * @return              An error code
479          * @param[in]   code                    The error code of CURLcode
480          */
481         static result ConvertErrorCode(CURLcode code);
482
483         /**
484          * Checks whether the error code is a ssl error.
485          *
486          * @return              @c true if he error code is a ssl error, @n
487          *                              else @c false
488          * @param[in]   code                    The error code of CURLcode
489          */
490         static bool IsSslError(CURLcode code);
491
492         /**
493          * Gets certificate list.
494          *
495          * @return              The certificate list <_HttpSslInfo>
496          */
497         static Tizen::Base::Collection::IList* GetSslCertList(void);
498
499         /**
500          * Sets a _HttpSslInfo
501          *
502          * @return              An error code
503          * @param[in]   sslInfo                         The instance of _HttpSslInfo
504          * @exception   E_OUT_OF_MEMORY         The memory is insufficient.
505          * @exception   E_SYSTEM                        A system error has occurred.
506          */
507         static result SetSslCertInfo(_HttpSslInfo& sslInfo);
508
509         /**
510          * Gets an instance of _HttpSslInfo searched by socket fd.
511          *
512          * @return              The instance of _HttpSslInfo
513          * @param[in]   socketFd                        The socket fd
514          * @exception   E_OUT_OF_MEMORY         The memory is insufficient.
515          * @exception   E_SYSTEM                        A system error has occurred.
516          * @remarks             The specific error code can be accessed using the GetLastResult() method.
517          */
518         static _HttpSslInfo* GetSslCertInfo(int socketFd);
519
520         /**
521          * Removes a _HttpSslInfo
522          *
523          * @return              An error code
524          * @param[in]   sslInfo                         The instance of _HttpSslInfo
525          * @exception   E_SYSTEM                        A system error has occurred.
526          */
527         static result RemoveSslCertInfo(_HttpSslInfo & sslInfo);
528
529 private:
530         /**
531          * This is the default constructor for this class.
532          */
533         _HttpUtility(void);
534
535         /**
536          * This is the destructor for this class.
537          */
538         virtual ~_HttpUtility(void);
539
540 private:
541         _HttpUtility(const _HttpUtility& rhs);
542         _HttpUtility& operator =(const _HttpUtility& rhs);
543
544 private:
545
546         // system variables for system. (singleton)
547         static bool __initialized;
548         static Tizen::Base::Runtime::Mutex* __pHttpMutex;
549         static Tizen::Base::Collection::IList* __pSslCertList;
550
551
552 }; // _HttpUtility
553
554 /**
555  * @class       _HttpSslInfo
556  * @brief       This class represents a SSL Information.
557  *
558  */
559 class _HttpSslInfo
560         : public Tizen::Base::Object
561 {
562
563 public:
564         _HttpSslInfo(int socketFd, int depth, const Tizen::Base::String& serverCert, const Tizen::Base::String& errorMessage);
565         _HttpSslInfo(int socketFd, _HttpTransactionEvent* pHttpTransactionEvent);
566         virtual ~_HttpSslInfo(void);
567
568     int GetSocketFd(void) const;
569     int GetDepth(void) const;
570     Tizen::Base::String GetErrorMessage(void) const;
571     Tizen::Base::String GetServerCert(void) const;
572     _HttpTransactionEvent* GetHttpTransactionEvent(void) const;
573     NetHttpCertificateVerificationFlag GetCertificateVerificationFlag(void) const;
574
575 private:
576         int __socketFd;
577         int __depth;
578         Tizen::Base::String __serverCert;
579         Tizen::Base::String __errorMessage;
580         _HttpTransactionEvent* __pHttpTransactionEvent;
581         NetHttpCertificateVerificationFlag __flag;
582
583 }; //_HttpSslInfo
584
585 } } } //Tizen::Net::Http
586 #endif // _FNET_HTTP_INTERNAL_HTTP_COMMON_H_