2 // Open Service Platform
3 // Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
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
9 // http://www.apache.org/licenses/LICENSE-2.0
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.
19 * @file FNetHttp_HttpCommon.h
20 * @brief This is the header file for defining common types and utility functions for %HTTP operations.
22 * This header file contains the definition of common types and utility functions for %HTTP operations.
25 #ifndef _FNET_HTTP_INTERNAL_HTTP_COMMON_H_
26 #define _FNET_HTTP_INTERNAL_HTTP_COMMON_H_
29 #include <FNetHttpHttpTypes.h>
30 #include <FBaseSysLog.h>
32 namespace Tizen { namespace Base
38 namespace Tizen { namespace Base { namespace Collection
43 namespace Tizen { namespace Base { namespace Runtime
48 namespace Tizen { namespace Net { namespace Http
51 class _HttpTransactionEvent;
54 * The User-Agent header name
56 _OSP_EXPORT_ extern const wchar_t _HTTP_USERAGENT_HEADER_NAME[];
59 * The Content-Length header name
61 _OSP_EXPORT_ extern const wchar_t _HTTP_CONTENT_LENGTH_HEADER_NAME[];
64 * The Content-Type header name
66 _OSP_EXPORT_ extern const wchar_t _HTTP_CONTENT_TYPE_HEADER_NAME[];
69 * The Transfer-Encoding header name
71 _OSP_EXPORT_ extern const wchar_t _HTTP_TRANSFER_ENCODING_HEADER_NAME[];
74 * The Expect header name
76 _OSP_EXPORT_ extern const wchar_t _HTTP_EXPECT_HEADER_NAME[];
81 _OSP_EXPORT_ extern const wchar_t _HTTP_SET_COOKIE_HEADER_NAME[];
84 * The Proxy-Authenticate name
86 _OSP_EXPORT_ extern const wchar_t _HTTP_PROXY_AUTHENTICATE_HEADER_NAME[];
89 * The WWW-Authenticate
91 _OSP_EXPORT_ extern const wchar_t _HTTP_WWW_AUTHENTICATE_HEADER_NAME[];
94 * The chunked header value
96 _OSP_EXPORT_ extern const wchar_t _HTTP_CHUNKED_HEADER_VALUE[];
99 * The protocol scheme of http://
101 _OSP_EXPORT_ extern const wchar_t _HTTP_PROTOCOL_SCHEME[];
104 * The protocol scheme of https://
106 _OSP_EXPORT_ extern const wchar_t _HTTPS_PROTOCOL_SCHEME[];
109 * The default charset of body part.
111 _OSP_EXPORT_ extern const wchar_t _HTTP_DEFAULT_BODY_CHARSET[];
114 * The prefix of multipart/form-data.
116 _OSP_EXPORT_ extern const wchar_t _MULTI_PART_FORM_DATA_PREFIX[];
119 * The default charset of string part.
121 _OSP_EXPORT_ extern const wchar_t _HTTP_STRING_PART_DEFAULT_CHARSET[];
124 * The default charset of file part.
126 _OSP_EXPORT_ extern const wchar_t _HTTP_FILE_PART_DEFAULT_CHARSET[];
129 * The character set of boundary
131 _OSP_EXPORT_ extern const wchar_t _HTTP_MULTI_PART_BOUNDARY_CHARACTER_SET[];
134 * The length of character set of boundary
136 static const int _HTTP_MULTI_PART_BOUNDARY_CHARACTER_SET_LENGTH = 62;
139 * The delimiter of cookie value
141 _OSP_EXPORT_ extern const wchar_t _HTTP_SET_COOKIE_DELIMITER[];
144 * The delimiter of cookie flag
146 _OSP_EXPORT_ extern const wchar_t _HTTP_SET_COOKIE_FLAG_DELIMITER[];
149 * The Comment flag name of Cookie
151 _OSP_EXPORT_ extern const wchar_t _HTTP_COOKIE_COMMENT_NAME[];
154 * The CommentURL flag name of Cookie
156 _OSP_EXPORT_ extern const wchar_t _HTTP_COOKIE_COMMENTURL_NAME[];
159 * The Discard flag name of Cookie
161 _OSP_EXPORT_ extern const wchar_t _HTTP_COOKIE_DISCARD_NAME[];
164 * The Expires flag name of Cookie
166 _OSP_EXPORT_ extern const wchar_t _HTTP_COOKIE_EXPIRES_NAME[];
169 * The Domain flag name of Cookie
171 _OSP_EXPORT_ extern const wchar_t _HTTP_COOKIE_DOMAIN_NAME[];
174 * The Max-Age flag name of Cookie
176 _OSP_EXPORT_ extern const wchar_t _HTTP_COOKIE_MAX_AGE_NAME[];
179 * The Path flag name of Cookie
181 _OSP_EXPORT_ extern const wchar_t _HTTP_COOKIE_PATH_NAME[];
184 * The Port flag name of Cookie
186 _OSP_EXPORT_ extern const wchar_t _HTTP_COOKIE_PORT_NAME[];
189 * The Secure flag name of Cookie
191 _OSP_EXPORT_ extern const wchar_t _HTTP_COOKIE_SECURE_NAME[];
194 * The Version flag name of Cookie
196 _OSP_EXPORT_ extern const wchar_t _HTTP_COOKIE_VERSION_NAME[];
199 * The HttpOnly flag name of Cookie
201 _OSP_EXPORT_ extern const wchar_t _HTTP_COOKIE_HTTP_ONLY[];
204 * The carriage return and line feed characters
206 _OSP_EXPORT_ extern const wchar_t _HTTP_CARRIAGE_RETURN_LINE_FEED[];
209 * The #HttpOnly_ prefix of curl
211 _OSP_EXPORT_ extern const wchar_t _CURL_COOKIE_HTTP_ONLY_PREFIX[];
214 * The path of cookie file ({application-home dir}/data/cookies.txt)
216 _OSP_EXPORT_ extern const wchar_t _HTTP_COOKIE_FILE[];
218 static const int _CURL_COOKIE_DOMAIN_NAME_INDEX = 0;
220 static const int _CURL_COOKIE_COOKIE_NAME_INDEX = 5;
222 static const int _CURL_COOKIE_COOKIE_VALUE_INDEX = 6;
224 static const int _COOKIE_PREFIX_LENGTH = 7;
226 static const long long _HTTP_INVALID_CONTENT_LENGTH = -1;
228 static const int _HTTP_INVALID_ID = -1;
231 * The maximum number of transactions allowed (NET_HTTP_SESSION_MODE_NORMAL mode)
233 static const int _MAX_HTTP_TRANSACTIONS_PER_SESSION_NORMAL = 1;
236 * The maximum number of transactions allowed (NET_HTTP_SESSION_MODE_PIPELINING mode)
238 static const int _MAX_HTTP_TRANSACTIONS_PER_SESSION_PIPE = 5;
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.
243 static const int _MAX_HTTP_TRANSACTIONS_PER_SESSION_MULTIPLE_HOST = 0;
246 * The maximum number of session that application can have.
248 static const int _MAX_HTTP_SESSIONS_PER_APPID_PER_PROTOCOL = 10;
251 * The length of multi-part boundary
253 static const int _HTTP_MULTIPART_BOUNDARY_LENGTH = 40;
256 * The default connection timeout
258 static const int _HTTP_DEFAULT_CONNECTION_TIMEOUT = 30;
261 * The http default port
263 static const int _HTTP_DEFAULT_PORT = 80;
266 * The event type of transaction
268 enum _HttpTransactionEventType
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
283 * The maximum size of header
285 static const int _HTTP_DEFAULT_HEADER_SIZE = 1024;
288 * The maximum limit of depth for certificate verification.
290 static const int _HTTP_CERT_VERIFICATION_DEPTH_LIMIT = 9;
293 * The maximum size of certificate subject.
295 static const int _HTTP_CERT_SUBJECT_SIZE = 256;
298 * The maximum size of body to read.
300 static const int _MAX_HTTP_BODY_SIZE = CURL_MAX_WRITE_SIZE;
303 * The length of multi-part boundary
305 static const int _HTTP_NETWORK_ERROR = -1;
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
319 * Checks whether the host address contains the protocol scheme such as "http://" or "https://".
321 * @return @c true if the host address contains the protocol scheme, @n
323 * @param[in] host The host address
325 static bool HasProtocolScheme(const Tizen::Base::String& host);
328 * Gets the protocol scheme.
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.
336 static Tizen::Base::String GetProtocolScheme(const Tizen::Base::String& host);
339 * Compares the values of the two hosts.
341 * @return @c true if the host part of @c host0 equal to the @c host1, @n
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.
349 static bool CompareHost(const Tizen::Base::String& host0, const Tizen::Base::String& host1);
352 * Gets the cookie file path.
354 * @return The cookie file path.
356 static Tizen::Base::String GetCookieFilePath(void);
359 * Converts NetHttpTransactionEventType to string name.
361 * @return The string of NetHttpTransactionEventType
362 * @param[in] type The type of NetHttpTransactionEventType
364 static const char* ConvertHttpTransactionEventTypeToString(_HttpTransactionEventType type);
367 * Converts NetHttpCertificateVerificationFlag to string name.
369 * @return The string of NetHttpCertificateVerificationFlag
370 * @param[in] flag The flag of NetHttpCertificateVerificationFlag
372 static const char* ConvertNetHttpCertificateVerificationFlagToString(NetHttpCertificateVerificationFlag flag);
375 * Adds a buffer to the header buffer.
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.
383 static result AddHeaderByteBufferToBuffer(Tizen::Base::ByteBuffer** ppSrcBuffer, const Tizen::Base::ByteBuffer& destBuffer);
386 * Adds a bytes to the header buffer.
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.
395 static result AddHeaderBytesToBuffer(Tizen::Base::ByteBuffer** ppSrcBuffer, const byte* pDestBytes, int destBytesSize);
398 * Adds a destination buffer to the source buffer.
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.
407 static result AddByteBufferToBuffer(Tizen::Base::ByteBuffer** ppSrcBuffer, const Tizen::Base::ByteBuffer& destBuffer, int defaultExtendedSize);
410 * Adds a bytes to the buffer.
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.
420 static result AddBytesToBuffer(Tizen::Base::ByteBuffer** ppSrcBuffer, const byte* pDestBytes, int destBytesSize, int defaultExtendedSize);
423 * Prints a curl error code.
425 * @param[in] code The error code of CURLMcode
427 static void PrintCurlMultiErrorCode(CURLMcode code);
430 * Destroy system variables for HTTP.
432 static void DestroyHttp(void);
435 * Once block for thread-safe singleton.
437 static void InitializeHttpOnce(void);
440 * Initialize system variables for HTTP.
442 static void InitializeHttp(void);
445 * Get the system mutex.
447 static Tizen::Base::Runtime::Mutex* GetHttpMutex(void);
450 * Converts the auth scheme of curl to the auth scheme of osp.
452 * @return The auth scheme of osp
453 * @param[in] isProxyAuth @c true if the @c curlAuthScheme is proxy auth, @n
455 * @param[in] curlAuthScheme The auth scheme of curl
457 static NetHttpAuthScheme GetHttpAuthScheme(bool isProxyAuth, long curlAuthScheme);
460 * Gets the name of auth scheme.
462 * @return The name of auth scheme
463 * @param[in] httpAuthScheme The auth scheme of osp
465 static const char* GetHttpAuthSchemeByString(NetHttpAuthScheme httpAuthScheme);
468 * Converts the auth scheme of osp to the auth scheme of curl.
470 * @return The auth scheme of curl
471 * @param[in] httpAuthScheme The auth scheme of osp
473 static long GetHttpCurlAuthScheme(NetHttpAuthScheme httpAuthScheme);
476 * Converts a curl error code to an osp error.
478 * @return An error code
479 * @param[in] code The error code of CURLcode
481 static result ConvertErrorCode(CURLcode code);
484 * Checks whether the error code is a ssl error.
486 * @return @c true if he error code is a ssl error, @n
488 * @param[in] code The error code of CURLcode
490 static bool IsSslError(CURLcode code);
493 * Gets certificate list.
495 * @return The certificate list <_HttpSslInfo>
497 static Tizen::Base::Collection::IList* GetSslCertList(void);
500 * Sets a _HttpSslInfo
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.
507 static result SetSslCertInfo(_HttpSslInfo& sslInfo);
510 * Gets an instance of _HttpSslInfo searched by socket fd.
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.
518 static _HttpSslInfo* GetSslCertInfo(int socketFd);
521 * Removes a _HttpSslInfo
523 * @return An error code
524 * @param[in] sslInfo The instance of _HttpSslInfo
525 * @exception E_SYSTEM A system error has occurred.
527 static result RemoveSslCertInfo(_HttpSslInfo & sslInfo);
531 * This is the default constructor for this class.
536 * This is the destructor for this class.
538 virtual ~_HttpUtility(void);
541 _HttpUtility(const _HttpUtility& rhs);
542 _HttpUtility& operator =(const _HttpUtility& rhs);
546 // system variables for system. (singleton)
547 static bool __initialized;
548 static Tizen::Base::Runtime::Mutex* __pHttpMutex;
549 static Tizen::Base::Collection::IList* __pSslCertList;
555 * @class _HttpSslInfo
556 * @brief This class represents a SSL Information.
560 : public Tizen::Base::Object
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);
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;
578 Tizen::Base::String __serverCert;
579 Tizen::Base::String __errorMessage;
580 _HttpTransactionEvent* __pHttpTransactionEvent;
581 NetHttpCertificateVerificationFlag __flag;
585 } } } //Tizen::Net::Http
586 #endif // _FNET_HTTP_INTERNAL_HTTP_COMMON_H_