From e845117fa049e65c558cd55907f47db0e459cbbd Mon Sep 17 00:00:00 2001 From: liuleimin Date: Mon, 26 May 2025 19:38:58 +0800 Subject: [PATCH] =?UTF-8?q?http=20c=E6=8E=A5=E5=8F=A3=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: liuleimin --- .../kits/c/common/netstack_hash_map.cpp | 32 ++-- interfaces/kits/c/net_http/include/net_http.h | 15 +- .../kits/c/net_http/include/net_http_type.h | 174 +++++++----------- interfaces/kits/c/net_http/src/net_http_c.cpp | 42 ++--- test/unittest/net_http/NetHttpTest.cpp | 20 +- .../unittest/net_http/NetStackHashMapTest.cpp | 24 +-- 6 files changed, 131 insertions(+), 176 deletions(-) diff --git a/interfaces/kits/c/common/netstack_hash_map.cpp b/interfaces/kits/c/common/netstack_hash_map.cpp index b90b25784..f77c68a40 100644 --- a/interfaces/kits/c/common/netstack_hash_map.cpp +++ b/interfaces/kits/c/common/netstack_hash_map.cpp @@ -78,13 +78,13 @@ static uint32_t NetstackResizeMap(Netstack_HashMap *map) { if (map->capacity >= MAX_MAP_CAPACITY) { NETSTACK_LOGE("map capacity reaches max, skip resize"); - return PARAMETER_ERROR; + return HTTP_NDK_PARAMETER_ERROR; } uint32_t newCapacity = map->capacity * 2; Netstack_HashMapEntry **newEntries = (Netstack_HashMapEntry **)calloc(newCapacity, sizeof(Netstack_HashMapEntry *)); if (newEntries == nullptr) { NETSTACK_LOGE("failed to insert map: no memory for resize"); - return OUT_OF_MEMORY; + return HTTP_NDK_OUT_OF_MEMORY; } map->size = 0; @@ -99,13 +99,13 @@ static uint32_t NetstackResizeMap(Netstack_HashMap *map) map->entries = newEntries; map->capacity = newCapacity; - return RESULT_OK; + return HTTP_NDK_RESULT_OK; } uint32_t Netstack_PutMapEntry(Netstack_HashMap *map, const char *key, void *value) { if (NetstackInvalidMap(map) || key == nullptr) { - return PARAMETER_ERROR; + return HTTP_NDK_PARAMETER_ERROR; } if (map->size >= map->capacity) { @@ -118,18 +118,18 @@ uint32_t Netstack_PutMapEntry(Netstack_HashMap *map, const char *key, void *valu Netstack_HashMapEntry *entry = (Netstack_HashMapEntry *)malloc(sizeof(Netstack_HashMapEntry)); if (entry == nullptr) { NETSTACK_LOGE("failed to alloc map entry"); - return OUT_OF_MEMORY; + return HTTP_NDK_OUT_OF_MEMORY; } entry->key = strdup(key); if (entry->key == nullptr) { free(entry); - return OUT_OF_MEMORY; + return HTTP_NDK_OUT_OF_MEMORY; } entry->value = value; entry->next = nullptr; map->entries[idx] = entry; map->size++; - return RESULT_OK; + return HTTP_NDK_RESULT_OK; } Netstack_HashMapEntry *ptr = map->entries[idx]; @@ -137,7 +137,7 @@ uint32_t Netstack_PutMapEntry(Netstack_HashMap *map, const char *key, void *valu // replace exist entry if (strcmp(ptr->key, key) == 0) { ptr->value = value; - return RESULT_OK; + return HTTP_NDK_RESULT_OK; } ptr = ptr->next; } @@ -145,18 +145,18 @@ uint32_t Netstack_PutMapEntry(Netstack_HashMap *map, const char *key, void *valu // insert after first entry Netstack_HashMapEntry *entry = (Netstack_HashMapEntry *)malloc(sizeof(Netstack_HashMapEntry)); if (entry == nullptr) { - return OUT_OF_MEMORY; + return HTTP_NDK_OUT_OF_MEMORY; } entry->key = strdup(key); if (entry->key == nullptr) { free(entry); - return OUT_OF_MEMORY; + return HTTP_NDK_OUT_OF_MEMORY; } entry->value = value; entry->next = map->entries[idx]->next; map->entries[idx]->next = entry; - return RESULT_OK; + return HTTP_NDK_RESULT_OK; } void *Netstack_GetMapEntry(Netstack_HashMap *map, const char *key) @@ -179,12 +179,12 @@ void *Netstack_GetMapEntry(Netstack_HashMap *map, const char *key) uint32_t Netstack_DeleteMapEntry(Netstack_HashMap *map, const char *key) { if (NetstackInvalidMap(map) || key == nullptr) { - return PARAMETER_ERROR; + return HTTP_NDK_PARAMETER_ERROR; } uint32_t idx = Netstack_Hash(key, map->capacity); if (map->entries[idx] == nullptr) { - return RESULT_OK; + return HTTP_NDK_RESULT_OK; } if (strcmp(map->entries[idx]->key, key) == 0) { @@ -195,7 +195,7 @@ uint32_t Netstack_DeleteMapEntry(Netstack_HashMap *map, const char *key) if (map->entries[idx] == nullptr) { map->size--; } - return RESULT_OK; + return HTTP_NDK_RESULT_OK; } Netstack_HashMapEntry *prev = map->entries[idx]; @@ -205,12 +205,12 @@ uint32_t Netstack_DeleteMapEntry(Netstack_HashMap *map, const char *key) prev->next = entry->next; free(entry->key); free(entry); - return RESULT_OK; + return HTTP_NDK_RESULT_OK; } prev = entry; entry = entry->next; } - return RESULT_OK; + return HTTP_NDK_RESULT_OK; } void Netstack_DestroyMapWithValue(Netstack_HashMap *map, Netstack_DestroyValueFunction destroyFunction) diff --git a/interfaces/kits/c/net_http/include/net_http.h b/interfaces/kits/c/net_http/include/net_http.h index 25cf8a3d9..24878f4da 100644 --- a/interfaces/kits/c/net_http/include/net_http.h +++ b/interfaces/kits/c/net_http/include/net_http.h @@ -21,7 +21,6 @@ * * @syscap SystemCapability.Communication.NetStack * @since 20 - * @version 1.0 */ /** @@ -32,7 +31,6 @@ * @kit NetworkKit * @syscap SystemCapability.Communication.NetStack * @since 20 - * @version 1.0 */ #ifndef NET_HTTP_H @@ -53,17 +51,15 @@ extern "C" { * @return Http_Headers* Pointer to {@link Http_Headers}. * @syscap SystemCapability.Communication.NetStack * @since 20 - * @version 1.0 */ Http_Headers *OH_Http_CreateHeaders(void); /** * @brief Destroys the headers of a request or response. * - * @param headers Pointer to the {@link Http_Headers} to be destroyed. + * @param headers Pointer to the {@link Http_Headers} to be destroyed, headers ends with null. * @syscap SystemCapability.Communication.NetStack * @since 20 - * @version 1.0 */ void OH_Http_DestroyHeaders(Http_Headers **headers); @@ -76,7 +72,6 @@ void OH_Http_DestroyHeaders(Http_Headers **headers); * @return uint32_t 0 - success. 401 - Parameter error. 2300027 - Out of memory. * @syscap SystemCapability.Communication.NetStack * @since 20 - * @version 1.0 */ uint32_t OH_Http_SetHeaderValue(struct Http_Headers *headers, const char *name, const char *value); @@ -88,7 +83,6 @@ uint32_t OH_Http_SetHeaderValue(struct Http_Headers *headers, const char *name, * @return Http_HeaderValue* Pointer to the obtained {@link Http_HeaderValue}. * @syscap SystemCapability.Communication.NetStack * @since 20 - * @version 1.0 */ Http_HeaderValue *OH_Http_GetHeaderValue(Http_Headers *headers, const char *name); @@ -99,17 +93,15 @@ Http_HeaderValue *OH_Http_GetHeaderValue(Http_Headers *headers, const char *name * @return Http_HeaderEntry* Pointers to all obtained key-value pairs {@link Http_HeaderEntry}. * @syscap SystemCapability.Communication.NetStack * @since 20 - * @version 1.0 */ Http_HeaderEntry *OH_Http_GetHeaderEntries(Http_Headers *headers); /** * @brief Destroys all key-value pairs obtained in {@link OH_Http_GetHeaderEntries}. * - * @param headerEntry Pointer to the {@link Http_HeaderEntry} to be destroyed. + * @param headerEntry Pointer to the {@link Http_HeaderEntry} to be destroyed, headerEntry ends with null. * @syscap SystemCapability.Communication.NetStack * @since 20 - * @version 1.0 */ void OH_Http_DestroyHeaderEntries(Http_HeaderEntry **headerEntry); @@ -120,7 +112,6 @@ void OH_Http_DestroyHeaderEntries(Http_HeaderEntry **headerEntry); * @return Pointer of HttpRequest if success; Null otherwise. * @syscap SystemCapability.Communication.NetStack * @since 20 - * @version 1.0 */ Http_Request *OH_Http_CreateRequest(const char *url); @@ -134,7 +125,6 @@ Http_Request *OH_Http_CreateRequest(const char *url); * @permission ohos.permission.INTERNET * @syscap SystemCapability.Communication.NetStack * @since 20 - * @version 1.0 */ int OH_Http_Request(Http_Request *request, Http_ResponseCallback callback, Http_EventsHandler handler); @@ -144,7 +134,6 @@ int OH_Http_Request(Http_Request *request, Http_ResponseCallback callback, Http_ * @param request Pointer to the http request {@link Http_Request}. * @syscap SystemCapability.Communication.NetStack * @since 20 - * @version 1.0 */ void OH_Http_Destroy(struct Http_Request **request); #ifdef __cplusplus diff --git a/interfaces/kits/c/net_http/include/net_http_type.h b/interfaces/kits/c/net_http/include/net_http_type.h index 0019faf84..dd6f9bbc0 100644 --- a/interfaces/kits/c/net_http/include/net_http_type.h +++ b/interfaces/kits/c/net_http/include/net_http_type.h @@ -20,7 +20,6 @@ * @brief Provides C APIs for the Http client module. * * @since 20 - * @version 1.0 */ /** @@ -31,7 +30,6 @@ * @kit NetworkKit * @syscap SystemCapability.Communication.NetStack * @since 20 - * @version 1.0 */ #ifndef NET_HTTP_TYPE_H @@ -49,171 +47,168 @@ extern "C" { * @brief Defines http error code. * * @since 20 - * @version 1.0 */ typedef enum Http_ErrCode { /** Operation success. */ - RESULT_OK = 0, + HTTP_NDK_RESULT_OK = 0, /** @brief Parameter error. */ - PARAMETER_ERROR = 401, + HTTP_NDK_PARAMETER_ERROR = 401, /** @brief Permission denied. */ - PERMISSION_DENIED = 201, + HTTP_NDK_PERMISSION_DENIED = 201, /** @brief Error code base. */ - NETSTACK_E_BASE = 2300000, + HTTP_NDK_NETSTACK_E_BASE = 2300000, /** @brief Unsupported protocol. */ - UNSUPPORTED_PROTOCOL = (NETSTACK_E_BASE + 1), + HTTP_NDK_UNSUPPORTED_PROTOCOL = (HTTP_NDK_NETSTACK_E_BASE + 1), /** @brief Invalid URL format or missing URL. */ - INVALID_URL = (NETSTACK_E_BASE + 3), + HTTP_NDK_INVALID_URL = (HTTP_NDK_NETSTACK_E_BASE + 3), /** @brief Failed to resolve the proxy name. */ - RESOLVE_PROXY_FAILED = (NETSTACK_E_BASE + 5), + HTTP_NDK_RESOLVE_PROXY_FAILED = (HTTP_NDK_NETSTACK_E_BASE + 5), /** @brief Failed to resolve the host name. */ - RESOLVE_HOST_FAILED = (NETSTACK_E_BASE + 6), + HTTP_NDK_RESOLVE_HOST_FAILED = (HTTP_NDK_NETSTACK_E_BASE + 6), /** @brief Failed to connect to the server. */ - CONNECT_SERVER_FAILED = (NETSTACK_E_BASE + 7), + HTTP_NDK_CONNECT_SERVER_FAILED = (HTTP_NDK_NETSTACK_E_BASE + 7), /** @brief Invalid server response. */ - INVALID_SERVER_RESPONSE = (NETSTACK_E_BASE + 8), + HTTP_NDK_INVALID_SERVER_RESPONSE = (HTTP_NDK_NETSTACK_E_BASE + 8), /** @brief Access to the remote resource denied. */ - ACCESS_REMOTE_DENIED = (NETSTACK_E_BASE + 9), + HTTP_NDK_ACCESS_REMOTE_DENIED = (HTTP_NDK_NETSTACK_E_BASE + 9), /** @brief Error in the HTTP2 framing layer. */ - HTTP2_FRAMING_ERROR = (NETSTACK_E_BASE + 16), + HTTP_NDK_HTTP2_FRAMING_ERROR = (HTTP_NDK_NETSTACK_E_BASE + 16), /** @brief Transferred a partial file. */ - TRANSFER_PARTIAL_FILE = (NETSTACK_E_BASE + 18), + HTTP_NDK_TRANSFER_PARTIAL_FILE = (HTTP_NDK_NETSTACK_E_BASE + 18), /** @brief Failed to write the received data to the disk or application. */ - WRITE_DATA_FAILED = (NETSTACK_E_BASE + 23), + HTTP_NDK_WRITE_DATA_FAILED = (HTTP_NDK_NETSTACK_E_BASE + 23), /** @brief Upload failed. */ - UPLOAD_FAILED = (NETSTACK_E_BASE + 25), + HTTP_NDK_UPLOAD_FAILED = (HTTP_NDK_NETSTACK_E_BASE + 25), /** @brief Failed to open or read local data from the file or application. */ - OPEN_LOCAL_DATA_FAILED = (NETSTACK_E_BASE + 26), + HTTP_NDK_OPEN_LOCAL_DATA_FAILED = (HTTP_NDK_NETSTACK_E_BASE + 26), /** @brief Out of memory. */ - OUT_OF_MEMORY = (NETSTACK_E_BASE + 27), + HTTP_NDK_OUT_OF_MEMORY = (HTTP_NDK_NETSTACK_E_BASE + 27), /** @brief Operation timeout. */ - OPERATION_TIMEOUT = (NETSTACK_E_BASE + 28), + HTTP_NDK_OPERATION_TIMEOUT = (HTTP_NDK_NETSTACK_E_BASE + 28), /** @brief The number of redirections reaches the maximum allowed. */ - REDIRECTIONS_TOO_LARGE = (NETSTACK_E_BASE + 47), + HTTP_NDK_REDIRECTIONS_TOO_LARGE = (HTTP_NDK_NETSTACK_E_BASE + 47), /** @brief The server returned nothing (no header or data). */ - SERVER_RETURNED_NOTHING = (NETSTACK_E_BASE + 52), + HTTP_NDK_SERVER_RETURNED_NOTHING = (HTTP_NDK_NETSTACK_E_BASE + 52), /** @brief Failed to send data to the peer. */ - SEND_DATA_FAILED = (NETSTACK_E_BASE + 55), + HTTP_NDK_SEND_DATA_FAILED = (HTTP_NDK_NETSTACK_E_BASE + 55), /** @brief Failed to receive data from the peer. */ - RECEIVE_DATA_FAILED = (NETSTACK_E_BASE + 56), + HTTP_NDK_RECEIVE_DATA_FAILED = (HTTP_NDK_NETSTACK_E_BASE + 56), /** @brief Local SSL certificate error. */ - SSL_CERTIFICATE_ERROR = (NETSTACK_E_BASE + 58), + HTTP_NDK_SSL_CERTIFICATE_ERROR = (HTTP_NDK_NETSTACK_E_BASE + 58), /** @brief The specified SSL cipher cannot be used. */ - SSL_CIPHER_USED_ERROR = (NETSTACK_E_BASE + 59), + HTTP_NDK_SSL_CIPHER_USED_ERROR = (HTTP_NDK_NETSTACK_E_BASE + 59), /** @brief Invalid SSL peer certificate or SSH remote key. */ - INVALID_SSL_PEER_CERT = (NETSTACK_E_BASE + 60), + HTTP_NDK_INVALID_SSL_PEER_CERT = (HTTP_NDK_NETSTACK_E_BASE + 60), /** @brief Invalid HTTP encoding format. */ - INVALID_ENCODING_FORMAT = (NETSTACK_E_BASE + 61), + HTTP_NDK_INVALID_ENCODING_FORMAT = (HTTP_NDK_NETSTACK_E_BASE + 61), /** @brief Maximum file size exceeded. */ - FILE_TOO_LARGE = (NETSTACK_E_BASE + 63), + HTTP_NDK_FILE_TOO_LARGE = (HTTP_NDK_NETSTACK_E_BASE + 63), /** @brief Remote disk full. */ - REMOTE_DISK_FULL = (NETSTACK_E_BASE + 70), + HTTP_NDK_REMOTE_DISK_FULL = (HTTP_NDK_NETSTACK_E_BASE + 70), /** @brief Remote file already exists. */ - REMOTE_FILE_EXISTS = (NETSTACK_E_BASE + 73), + HTTP_NDK_REMOTE_FILE_EXISTS = (HTTP_NDK_NETSTACK_E_BASE + 73), /** @brief The SSL CA certificate does not exist or is inaccessible. */ - SSL_CA_NOT_EXIST = (NETSTACK_E_BASE + 77), + HTTP_NDK_SSL_CA_NOT_EXIST = (HTTP_NDK_NETSTACK_E_BASE + 77), /** @brief Remote file not found. */ - REMOTE_FILE_NOT_FOUND = (NETSTACK_E_BASE + 78), + HTTP_NDK_REMOTE_FILE_NOT_FOUND = (HTTP_NDK_NETSTACK_E_BASE + 78), /** @brief Authentication error. */ - AUTHENTICATION_ERROR = (NETSTACK_E_BASE + 94), + HTTP_NDK_AUTHENTICATION_ERROR = (HTTP_NDK_NETSTACK_E_BASE + 94), /** @brief It is not allowed to access this domain. */ - ACCESS_DOMAIN_NOT_ALLOWED = (NETSTACK_E_BASE + 998), + HTTP_NDK_ACCESS_DOMAIN_NOT_ALLOWED = (HTTP_NDK_NETSTACK_E_BASE + 998), /** @brief Unknown error. */ - UNKNOWN_ERROR = (NETSTACK_E_BASE + 999) + HTTP_NDK_UNKNOWN_ERROR = (HTTP_NDK_NETSTACK_E_BASE + 999) } Http_ErrCode; /** * @brief Defines http response code. * * @since 20 - * @version 1.0 */ typedef enum Http_ResponseCode { /** @brief The request was successful. */ HTTP_OK = 200, /** @brief Successfully requested and created a new resource. */ - CREATED = 201, + HTTP_CREATED = 201, /** @brief The request has been accepted but has not been processed completely. */ - ACCEPTED = 202, + HTTP_ACCEPTED = 202, /** @brief Unauthorized information. The request was successful. */ - NOT_AUTHORITATIVE = 203, + HTTP_NOT_AUTHORITATIVE = 203, /** @brief No content. The server successfully processed, but did not return content. */ - NO_CONTENT = 204, + HTTP_NO_CONTENT = 204, /** @brief Reset the content. */ - RESET = 205, + HTTP_RESET = 205, /** @brief Partial content. The server successfully processed some GET requests. */ - PARTIAL = 206, + HTTP_PARTIAL = 206, /** @brief Multiple options. */ - MULT_CHOICE = 300, + HTTP_MULT_CHOICE = 300, /** * @brief Permanently move. The requested resource has been permanently moved to a new URI, * and the returned information will include the new URI. The browser will automatically redirect to the new URI. */ - MOVED_PERM = 301, + HTTP_MOVED_PERM = 301, /** @brief Temporary movement. */ - MOVED_TEMP = 302, + HTTP_MOVED_TEMP = 302, /** @brief View other addresses. */ - SEE_OTHER = 303, + HTTP_SEE_OTHER = 303, /** @brief Not modified. */ - NOT_MODIFIED = 304, + HTTP_NOT_MODIFIED = 304, /** @brief Using proxies. */ - USE_PROXY = 305, + HTTP_USE_PROXY = 305, /** @brief The server cannot understand the syntax error error requested by the client. */ - BAD_REQUEST = 400, + HTTP_BAD_REQUEST = 400, /** @brief Request for user authentication. */ - UNAUTHORIZED = 401, + HTTP_UNAUTHORIZED = 401, /** @brief Reserved for future use. */ - PAYMENT_REQUIRED = 402, + HTTP_PAYMENT_REQUIRED = 402, /** @brief The server understands the request from the requesting client, but refuses to execute it. */ - FORBIDDEN = 403, + HTTP_FORBIDDEN = 403, /** @brief The server was unable to find resources (web pages) based on the client's request. */ - NOT_FOUND = 404, + HTTP_NOT_FOUND = 404, /** @brief The method in the client request is prohibited. */ - BAD_METHOD = 405, + HTTP_BAD_METHOD = 405, /** @brief The server unabled to complete request based on the content characteristics requested by the client. */ - NOT_ACCEPTABLE = 406, + HTTP_NOT_ACCEPTABLE = 406, /** @brief Request authentication of the proxy's identity. */ - PROXY_AUTH = 407, + HTTP_PROXY_AUTH = 407, /** @brief The request took too long and timed out. */ - CLIENT_TIMEOUT = 408, + HTTP_CLIENT_TIMEOUT = 408, /** * @brief The server may have returned this code when completing the client's PUT request, * as there was a conflict when the server was processing the request. */ - CONFLICT = 409, + HTTP_CONFLICT = 409, /** @brief The resource requested by the client no longer exists. */ - GONE = 410, + HTTP_GONE = 410, /** @brief The server is unable to process request information sent by the client without Content Length. */ - LENGTH_REQUIRED = 411, + HTTP_LENGTH_REQUIRED = 411, /** @brief The prerequisite for requesting information from the client is incorrect. */ - PRECON_FAILED = 412, + HTTP_PRECON_FAILED = 412, /** @brief The request was rejected because the requested entity was too large for the server to process. */ - ENTITY_TOO_LARGE = 413, + HTTP_ENTITY_TOO_LARGE = 413, /** @brief The requested URI is too long (usually a URL) and the server cannot process it. */ - REQ_TOO_LONG = 414, + HTTP_REQ_TOO_LONG = 414, /** @brief The server is unable to process the requested format. */ - UNSUPPORTED_TYPE = 415, + HTTP_UNSUPPORTED_TYPE = 415, /** @brief Requested Range not satisfiable. */ - RANGE_NOT_SATISFIABLE = 416, + HTTP_RANGE_NOT_SATISFIABLE = 416, /** @brief Internal server error, unable to complete the request. */ - INTERNAL_ERROR = 500, + HTTP_INTERNAL_ERROR = 500, /** @brief * The server does not support the requested functionality and cannot complete the request. */ - NOT_IMPLEMENTED = 501, + HTTP_NOT_IMPLEMENTED = 501, /** @brief The server acting as a gateway or proxy received an invalid request from the remote server. */ - BAD_GATEWAY = 502, + HTTP_BAD_GATEWAY = 502, /** @brief Due to overload or system maintenance, the server is temporarily unable to process client requests. */ - UNAVAILABLE = 503, + HTTP_UNAVAILABLE = 503, /** @brief The server acting as gateway did not obtain requests from the remote server in a timely manner. */ - GATEWAY_TIMEOUT = 504, + HTTP_GATEWAY_TIMEOUT = 504, /** @brief The version of the HTTP protocol requested by the server. */ - VERSION = 505 + HTTP_VERSION = 505 } Http_ResponseCode; /** * @brief Buffer. * * @since 20 - * @version 1.0 */ typedef struct Http_Buffer { /** Content. Buffer will not be copied. */ @@ -226,7 +221,6 @@ typedef struct Http_Buffer { * @brief Defines the address Family. * * @since 20 - * @version 1.0 */ typedef enum Http_AddressFamilyType { /** Default, The system automatically selects the IPv4 or IPv6 address of the domain name. */ @@ -241,7 +235,6 @@ typedef enum Http_AddressFamilyType { * @brief HTTP get method. * * @since 20 - * @version 1.0 */ #define NET_HTTP_METHOD_GET "GET" @@ -249,7 +242,6 @@ typedef enum Http_AddressFamilyType { * @brief HTTP head method. * * @since 20 - * @version 1.0 */ #define NET_HTTPMETHOD_HEAD "HEAD" @@ -257,7 +249,6 @@ typedef enum Http_AddressFamilyType { * @brief HTTP options method. * * @since 20 - * @version 1.0 */ #define NET_HTTPMETHOD_OPTIONS "OPTIONS" @@ -265,13 +256,11 @@ typedef enum Http_AddressFamilyType { * @brief HTTP trace method. * * @since 20 - * @version 1.0 */ #define NET_HTTPMETHOD_TRACE "TRACE" /** * @brief HTTP delete method. * @since 20 - * @version 1.0 */ #define NET_HTTPMETHOD_DELETE "DELETE" @@ -279,7 +268,6 @@ typedef enum Http_AddressFamilyType { * @brief HTTP post method. * * @since 20 - * @version 1.0 */ #define NET_HTTP_METHOD_POST "POST" @@ -287,7 +275,6 @@ typedef enum Http_AddressFamilyType { * @brief HTTP put method. * * @since 20 - * @version 1.0 */ #define NET_HTTP_METHOD_PUT "PUT" @@ -295,7 +282,6 @@ typedef enum Http_AddressFamilyType { * @brief HTTP connect method. * * @since 20 - * @version 1.0 */ #define NET_HTTP_METHOD_PATCH "CONNECT" @@ -303,7 +289,6 @@ typedef enum Http_AddressFamilyType { * @brief Defines the HTTP version. * * @since 20 - * @version 1.0 */ typedef enum Http_HttpProtocol { /** Default choose by curl. */ @@ -320,7 +305,6 @@ typedef enum Http_HttpProtocol { * @brief Defines the Cert Type. * * @since 20 - * @version 1.0 */ typedef enum Http_CertType { /** PEM Cert Type. */ @@ -335,7 +319,6 @@ typedef enum Http_CertType { * @brief Headers of the request or response. * * @since 20 - * @version 1.0 */ typedef struct Http_Headers Http_Headers; @@ -343,7 +326,6 @@ typedef struct Http_Headers Http_Headers; * @brief The value type of the header map of the request or response. * * @since 20 - * @version 1.0 */ typedef struct Http_HeaderValue { /** Value. */ @@ -356,7 +338,6 @@ typedef struct Http_HeaderValue { * @brief All key-value pairs of the headers of the request or response. * * @since 20 - * @version 1.0 */ typedef struct Http_HeaderEntry { /** Key. */ @@ -372,7 +353,6 @@ typedef struct Http_HeaderEntry { * client's identification. * * @since 20 - * @version 1.0 */ typedef struct Http_ClientCert { /** A path to a client certificate. */ @@ -389,7 +369,6 @@ typedef struct Http_ClientCert { * @brief Proxy type. Used to distinguish different proxy configurations. * * @since 20 - * @version 1.0 */ typedef enum Http_ProxyType { /** No proxy */ @@ -404,7 +383,6 @@ typedef enum Http_ProxyType { * @brief Custom proxy configuration. * * @since 20 - * @version 1.0 */ typedef struct Http_CustomProxy { /** Indicates the URL of the proxy server. If you do not set port explicitly, port will be 1080. */ @@ -417,7 +395,6 @@ typedef struct Http_CustomProxy { * @brief Proxy configuration. * * @since 20 - * @version 1.0 */ typedef struct Http_Proxy { /** Distinguish the proxy type used by the request, see {@link Http_ProxyType}. */ @@ -430,7 +407,6 @@ typedef struct Http_Proxy { * @brief Response timing information. It will be collected in {@link Http_Response.performanceTiming}. * * @since 20 - * @version 1.0 */ typedef struct Http_PerformanceTiming { /** The total time in milliseconds for the HTTP transfer, including name resolving, TCP connect etc. */ @@ -453,7 +429,6 @@ typedef struct Http_PerformanceTiming { * @brief Defines the parameters for http request options. * * @since 20 - * @version 1.0 */ typedef struct Http_RequestOptions { /** Request method. */ @@ -493,7 +468,6 @@ typedef struct Http_RequestOptions { * @brief Defines the parameters for http response. * * @since 20 - * @version 1.0 */ typedef struct Http_Response { /** Response body, see {@link Http_Buffer}. */ @@ -511,7 +485,6 @@ typedef struct Http_Response { * * @param response Indicates the response to be deleted. It is a pointer that points to {@link Http_Response}. * @since 20 - * @version 1.0 */ void (*destroyResponse)(struct Http_Response **response); } Http_Response; @@ -520,7 +493,6 @@ typedef struct Http_Response { * @brief Http request. * * @since 20 - * @version 1.0 */ typedef struct Http_Request { /** The request id for every single request. Generated by system. */ @@ -537,7 +509,6 @@ typedef struct Http_Request { * @param response Http response struct, see {@link Http_Response}. * @param errCode Response error code. * @since 20 - * @version 1.0 */ typedef void (*Http_ResponseCallback)(struct Http_Response *response, uint32_t errCode); @@ -547,7 +518,6 @@ typedef void (*Http_ResponseCallback)(struct Http_Response *response, uint32_t e * @param data Response body. * @return size_t the length of response body. * @since 20 - * @version 1.0 */ typedef size_t (*Http_OnDataReceiveCallback)(const char *data); @@ -557,7 +527,6 @@ typedef size_t (*Http_OnDataReceiveCallback)(const char *data); * @param totalSize total size. * @param transferredSize transferred size. * @since 20 - * @version 1.0 */ typedef void (*Http_OnProgressCallback)(uint64_t totalSize, uint64_t transferredSize); @@ -566,15 +535,13 @@ typedef void (*Http_OnProgressCallback)(uint64_t totalSize, uint64_t transferred * * @param headers Headers of the received requests, which points to the pointer of {@link Http_Headers}. * @since 20 - * @version 1.0 */ typedef void (*Http_OnHeaderReceiveCallback)(Http_Headers *headers); /** - * @brief Empty callback function for requested DataEnd or Canceled event callback。 + * @brief Empty callback function for requested DataEnd or Canceled event callback * * @since 20 - * @version 1.0 */ typedef void (*Http_OnVoidCallback)(void); @@ -582,7 +549,6 @@ typedef void (*Http_OnVoidCallback)(void); * @brief Callbacks to watch different events. * * @since 20 - * @version 1.0 */ typedef struct Http_EventsHandler { /** Callback function when the response body is received */ diff --git a/interfaces/kits/c/net_http/src/net_http_c.cpp b/interfaces/kits/c/net_http/src/net_http_c.cpp index f06740d57..274b8f44e 100644 --- a/interfaces/kits/c/net_http/src/net_http_c.cpp +++ b/interfaces/kits/c/net_http/src/net_http_c.cpp @@ -97,19 +97,19 @@ static char *OH_Http_ToLowerCase(const char *str) uint32_t OH_Http_SetHeaderValue(Http_Headers *headers, const char *name, const char *value) { if (headers == nullptr || headers->fields == nullptr || name == nullptr || value == nullptr) { - return PARAMETER_ERROR; + return HTTP_NDK_PARAMETER_ERROR; } char *lowerName = OH_Http_ToLowerCase(name); if (lowerName == nullptr) { - return OUT_OF_MEMORY; + return HTTP_NDK_OUT_OF_MEMORY; } Http_HeaderValue *existValue = (Http_HeaderValue *)Netstack_GetMapEntry(headers->fields, lowerName); Http_HeaderValue *previous = existValue; while (existValue != nullptr) { if (strcmp(existValue->value, value) == 0) { free(lowerName); - return RESULT_OK; + return HTTP_NDK_RESULT_OK; } previous = existValue; existValue = existValue->next; @@ -119,18 +119,18 @@ uint32_t OH_Http_SetHeaderValue(Http_Headers *headers, const char *name, const c if (headerValue == nullptr) { NETSTACK_LOGE("failed to alloc memory for header value"); free(lowerName); - return OUT_OF_MEMORY; + return HTTP_NDK_OUT_OF_MEMORY; } headerValue->value = strdup(value); if (headerValue->value == nullptr) { free(headerValue); free(lowerName); - return OUT_OF_MEMORY; + return HTTP_NDK_OUT_OF_MEMORY; } if (previous == nullptr) { uint32_t res = Netstack_PutMapEntry(headers->fields, lowerName, headerValue); - if (res != RESULT_OK) { + if (res != HTTP_NDK_RESULT_OK) { free(headerValue->value); free(headerValue); } @@ -139,7 +139,7 @@ uint32_t OH_Http_SetHeaderValue(Http_Headers *headers, const char *name, const c } previous->next = headerValue; free(lowerName); - return RESULT_OK; + return HTTP_NDK_RESULT_OK; } Http_HeaderValue *OH_Http_GetHeaderValue(Http_Headers *headers, const char *name) @@ -375,8 +375,8 @@ void OH_Http_RequestOnSuccess(std::shared_ptr httpClientTask, NETSTACK_LOGI("OnSuccess. code=%{public}d", response.GetResponseCode()); Http_Response *resp = (Http_Response *)calloc(1, sizeof(Http_Response)); if (resp == nullptr) { - callback(nullptr, OUT_OF_MEMORY); - return OUT_OF_MEMORY; + callback(nullptr, HTTP_NDK_OUT_OF_MEMORY); + return HTTP_NDK_OUT_OF_MEMORY; } resp->responseCode = static_cast(response.GetResponseCode()); resp->cookies = const_cast(response.GetCookies().data()); @@ -385,9 +385,9 @@ void OH_Http_RequestOnSuccess(std::shared_ptr httpClientTask, Http_PerformanceTiming *performanceTiming = (Http_PerformanceTiming *)calloc(1, sizeof(Http_PerformanceTiming)); if (performanceTiming == nullptr) { - callback(nullptr, OUT_OF_MEMORY); + callback(nullptr, HTTP_NDK_OUT_OF_MEMORY); free(resp); - return OUT_OF_MEMORY; + return HTTP_NDK_OUT_OF_MEMORY; } performanceTiming->dnsTiming = response.GetPerformanceTiming().dnsTiming; performanceTiming->tcpTiming = response.GetPerformanceTiming().connectTiming; @@ -406,7 +406,7 @@ void OH_Http_RequestOnSuccess(std::shared_ptr httpClientTask, if (handler.onDataEnd != nullptr) { handler.onDataEnd(); } - return RESULT_OK; + return HTTP_NDK_RESULT_OK; }); } @@ -422,7 +422,7 @@ void OH_Http_RequestOnCancel(std::shared_ptr httpClientTask, Htt if (handler.onCanceled != nullptr) { handler.onCanceled(); } - return RESULT_OK; + return HTTP_NDK_RESULT_OK; }); } @@ -439,8 +439,8 @@ void OH_Http_RequestOnFail(std::shared_ptr httpClientTask, response.GetResponseCode(), error.GetErrorCode()); Http_Response *resp = (Http_Response *)calloc(1, sizeof(Http_Response)); if (resp == nullptr) { - callback(nullptr, OUT_OF_MEMORY); - return OUT_OF_MEMORY; + callback(nullptr, HTTP_NDK_OUT_OF_MEMORY); + return HTTP_NDK_OUT_OF_MEMORY; } resp->responseCode = static_cast(response.GetResponseCode()); resp->destroyResponse = OH_Http_DestroyResponse; @@ -448,7 +448,7 @@ void OH_Http_RequestOnFail(std::shared_ptr httpClientTask, if (handler.onDataEnd != nullptr) { handler.onDataEnd(); } - return RESULT_OK; + return HTTP_NDK_RESULT_OK; }); } @@ -463,7 +463,7 @@ void OH_Http_RequestOnDataReceive(std::shared_ptr httpClientTask if (handler.onDataReceive != nullptr) { handler.onDataReceive(reinterpret_cast(data)); } - return RESULT_OK; + return HTTP_NDK_RESULT_OK; }); } @@ -479,7 +479,7 @@ void OH_Http_RequestOnHeadersReceive(std::shared_ptr httpClientT Http_Headers *headers = OH_Http_ToCHeaders(headerWithSetCookie); handler.onHeadersReceive(headers); } - return RESULT_OK; + return HTTP_NDK_RESULT_OK; }); } @@ -501,7 +501,7 @@ void OH_Http_RequestOnProgress(std::shared_ptr httpClientTask, H handler.onDownloadProgress(dlTotal, dlNow); } } - return RESULT_OK; + return HTTP_NDK_RESULT_OK; }); } @@ -510,7 +510,7 @@ int OH_Http_Request(Http_Request *request, Http_ResponseCallback callback, Http_ NETSTACK_LOGI("OH_Http_Request enter"); if (request == nullptr || callback == nullptr) { NETSTACK_LOGE("OH_Http_Request request or callback is nullptr"); - return OUT_OF_MEMORY; + return HTTP_NDK_OUT_OF_MEMORY; } HttpClientRequest httpReq; httpReq.SetURL(request->url); @@ -521,7 +521,7 @@ int OH_Http_Request(Http_Request *request, Http_ResponseCallback callback, Http_ auto httpClientTask = session.CreateTask(httpReq); if (httpClientTask == nullptr) { NETSTACK_LOGE("OH_Http_Request httpClientTask is nullptr"); - return OUT_OF_MEMORY; + return HTTP_NDK_OUT_OF_MEMORY; } OH_Http_RequestOnSuccess(httpClientTask, callback, handler); OH_Http_RequestOnCancel(httpClientTask, handler); diff --git a/test/unittest/net_http/NetHttpTest.cpp b/test/unittest/net_http/NetHttpTest.cpp index 8d432b6e8..cabda1b99 100644 --- a/test/unittest/net_http/NetHttpTest.cpp +++ b/test/unittest/net_http/NetHttpTest.cpp @@ -78,7 +78,7 @@ HWTEST_F(NetHttpTest, DestroyHeaders001, TestSize.Level1) { Http_Headers *header = OH_Http_CreateHeaders(); EXPECT_TRUE(header != nullptr); - EXPECT_EQ(OH_Http_SetHeaderValue(header, "key", "value"), RESULT_OK); + EXPECT_EQ(OH_Http_SetHeaderValue(header, "key", "value"), HTTP_NDK_RESULT_OK); OH_Http_DestroyHeaders(&header); } @@ -102,15 +102,15 @@ HWTEST_F(NetHttpTest, ToLowerCase001, TestSize.Level1) HWTEST_F(NetHttpTest, SetHeaderValue001, TestSize.Level1) { uint32_t ret = OH_Http_SetHeaderValue(nullptr, "key", "test"); - EXPECT_EQ(ret, PARAMETER_ERROR); + EXPECT_EQ(ret, HTTP_NDK_PARAMETER_ERROR); Http_Headers *header = OH_Http_CreateHeaders(); EXPECT_TRUE(header != nullptr); ret = OH_Http_SetHeaderValue(header, nullptr, "test"); - EXPECT_EQ(ret, PARAMETER_ERROR); + EXPECT_EQ(ret, HTTP_NDK_PARAMETER_ERROR); ret = OH_Http_SetHeaderValue(header, "key2", nullptr); - EXPECT_EQ(ret, PARAMETER_ERROR); + EXPECT_EQ(ret, HTTP_NDK_PARAMETER_ERROR); OH_Http_DestroyHeaders(&header); } @@ -121,7 +121,7 @@ HWTEST_F(NetHttpTest, SetHeaderValue002, TestSize.Level1) OH_Http_SetHeaderValue(header, "key1", "test1"); uint32_t ret = OH_Http_SetHeaderValue(header, "key2", "test2"); - EXPECT_EQ(ret, RESULT_OK); + EXPECT_EQ(ret, HTTP_NDK_RESULT_OK); OH_Http_DestroyHeaders(&header); } @@ -132,7 +132,7 @@ HWTEST_F(NetHttpTest, SetHeaderValue003, TestSize.Level1) OH_Http_SetHeaderValue(header, "key1", "test1"); uint32_t ret = OH_Http_SetHeaderValue(header, "key1", "test1"); - EXPECT_EQ(ret, RESULT_OK); + EXPECT_EQ(ret, HTTP_NDK_RESULT_OK); OH_Http_DestroyHeaders(&header); } @@ -143,7 +143,7 @@ HWTEST_F(NetHttpTest, SetHeaderValue004, TestSize.Level1) OH_Http_SetHeaderValue(header, "key1", "test1"); uint32_t ret = OH_Http_SetHeaderValue(header, "key1", "test2"); - EXPECT_EQ(ret, RESULT_OK); + EXPECT_EQ(ret, HTTP_NDK_RESULT_OK); OH_Http_DestroyHeaders(&header); } @@ -155,7 +155,7 @@ HWTEST_F(NetHttpTest, SetHeaderValue005, TestSize.Level1) OH_Http_SetHeaderValue(header, "key1", "test1"); header->fields->capacity = MAX_MAP_CAPACITY + 1; uint32_t ret = OH_Http_SetHeaderValue(header, "key1", "test2"); - EXPECT_EQ(ret, PARAMETER_ERROR); + EXPECT_EQ(ret, HTTP_NDK_PARAMETER_ERROR); OH_Http_DestroyHeaders(&header); } @@ -393,8 +393,8 @@ HWTEST_F(NetHttpTest, HttpRequest001, TestSize.Level1) Http_ResponseCallback callback = testResponseCallback; Http_Request *request = OH_Http_CreateRequest("https://www.baidu.com"); Http_EventsHandler handler; - EXPECT_EQ(OH_Http_Request(nullptr, callback, handler), OUT_OF_MEMORY); - EXPECT_EQ(OH_Http_Request(request, nullptr, handler), OUT_OF_MEMORY); + EXPECT_EQ(OH_Http_Request(nullptr, callback, handler), HTTP_OUT_OF_MEMORY); + EXPECT_EQ(OH_Http_Request(request, nullptr, handler), HTTP_OUT_OF_MEMORY); OH_Http_Destroy(&request); } diff --git a/test/unittest/net_http/NetStackHashMapTest.cpp b/test/unittest/net_http/NetStackHashMapTest.cpp index d681ec254..91fd146f9 100644 --- a/test/unittest/net_http/NetStackHashMapTest.cpp +++ b/test/unittest/net_http/NetStackHashMapTest.cpp @@ -60,7 +60,7 @@ HWTEST_F(NetstackHashMapTest, NetstackInvalidMapTest001, TestSize.Level1) map = NULL; const char *value = "value"; uint32_t ret = Netstack_PutMapEntry(map, "key", (void *)value); - EXPECT_EQ(ret, PARAMETER_ERROR); + EXPECT_EQ(ret, HTTP_NDK_PARAMETER_ERROR); Netstack_DestroyMap(map); } @@ -70,7 +70,7 @@ HWTEST_F(NetstackHashMapTest, NetstackInvalidMapTest002, TestSize.Level1) map->entries = NULL; char *value = strdup("value"); uint32_t ret = Netstack_PutMapEntry(map, "key", value); - EXPECT_EQ(ret, PARAMETER_ERROR); + EXPECT_EQ(ret, HTTP_NDK_PARAMETER_ERROR); Netstack_DestroyMap(map); } @@ -80,7 +80,7 @@ HWTEST_F(NetstackHashMapTest, NetstackInvalidMapTest003, TestSize.Level1) map->capacity = DEFAULT_MAP_CAPACITY - 1; const char *value = "value"; uint32_t ret = Netstack_PutMapEntry(map, "key", (void *)value); - EXPECT_EQ(ret, PARAMETER_ERROR); + EXPECT_EQ(ret, HTTP_NDK_PARAMETER_ERROR); Netstack_DestroyMap(map); } @@ -90,7 +90,7 @@ HWTEST_F(NetstackHashMapTest, NetstackInvalidMapTest004, TestSize.Level1) map->capacity = MAX_MAP_CAPACITY + 1; const char *value = "value"; uint32_t ret = Netstack_PutMapEntry(map, "key", (void *)value); - EXPECT_EQ(ret, PARAMETER_ERROR); + EXPECT_EQ(ret, HTTP_NDK_PARAMETER_ERROR); Netstack_DestroyMap(map); } @@ -121,7 +121,7 @@ HWTEST_F(NetstackHashMapTest, NetstackPutMapEntryTest001, TestSize.Level1) EXPECT_TRUE(map != nullptr); char *key = NULL; const char *value = "value"; - EXPECT_EQ(Netstack_PutMapEntry(map, key, (void *)value), PARAMETER_ERROR); + EXPECT_EQ(Netstack_PutMapEntry(map, key, (void *)value), HTTP_NDK_PARAMETER_ERROR); Netstack_DestroyMap(map); } @@ -131,7 +131,7 @@ HWTEST_F(NetstackHashMapTest, NetstackPutMapEntryTest002, TestSize.Level1) map = NULL; const char *key = "key"; const char *value = "value"; - EXPECT_EQ(Netstack_PutMapEntry(map, key, (void *)value), PARAMETER_ERROR); + EXPECT_EQ(Netstack_PutMapEntry(map, key, (void *)value), HTTP_NDK_PARAMETER_ERROR); Netstack_DestroyMap(map); } @@ -152,7 +152,7 @@ HWTEST_F(NetstackHashMapTest, NetstackPutMapEntryTest004, TestSize.Level1) Netstack_HashMap *map = CreateMap(); const char *key = "key"; const char *value = "value"; - EXPECT_EQ(Netstack_PutMapEntry(map, key, (void *)value), RESULT_OK); + EXPECT_EQ(Netstack_PutMapEntry(map, key, (void *)value), HTTP_NDK_RESULT_OK); Netstack_DestroyMap(map); } @@ -163,7 +163,7 @@ HWTEST_F(NetstackHashMapTest, NetstackPutMapEntryTest005, TestSize.Level1) const char *value = "value"; const char *value2 = "value2"; Netstack_PutMapEntry(map, key, (void *)value2); - EXPECT_EQ(Netstack_PutMapEntry(map, key, (void *)value), RESULT_OK); + EXPECT_EQ(Netstack_PutMapEntry(map, key, (void *)value), HTTP_NDK_RESULT_OK); EXPECT_EQ(Netstack_GetMapEntry(map, key), "value"); Netstack_DestroyMap(map); } @@ -199,7 +199,7 @@ HWTEST_F(NetstackHashMapTest, NetstackDeleteMapEntryTest001, TestSize.Level1) Netstack_HashMap *map = CreateMap(); map->capacity = MAX_MAP_CAPACITY + 1; const char *key = "a"; - EXPECT_EQ(Netstack_DeleteMapEntry(map, key), PARAMETER_ERROR); + EXPECT_EQ(Netstack_DeleteMapEntry(map, key), HTTP_NDK_PARAMETER_ERROR); Netstack_DestroyMap(map); } @@ -207,7 +207,7 @@ HWTEST_F(NetstackHashMapTest, NetstackDeleteMapEntryTest002, TestSize.Level1) { Netstack_HashMap *map = CreateMap(); char *key = NULL; - EXPECT_EQ(Netstack_DeleteMapEntry(map, key), PARAMETER_ERROR); + EXPECT_EQ(Netstack_DeleteMapEntry(map, key), HTTP_NDK_PARAMETER_ERROR); Netstack_DestroyMap(map); } @@ -218,7 +218,7 @@ HWTEST_F(NetstackHashMapTest, NetstackDeleteMapEntryTest003, TestSize.Level1) const char *value = "value"; Netstack_PutMapEntry(map, key, (void *)value); int tempSize = map->size; - EXPECT_EQ(Netstack_DeleteMapEntry(map, key), RESULT_OK); + EXPECT_EQ(Netstack_DeleteMapEntry(map, key), HTTP_NDK_RESULT_OK); EXPECT_EQ(tempSize, map->size + 1); Netstack_DestroyMap(map); } @@ -235,7 +235,7 @@ HWTEST_F(NetstackHashMapTest, NetstackDeleteMapEntryTest003, TestSize.Level1) Netstack_PutMapEntry(map, key2, (void *)value); Netstack_PutMapEntry(map, key3, (void *)value); int tempSize = map->size; - EXPECT_EQ(Netstack_DeleteMapEntry(map, key1), RESULT_OK); + EXPECT_EQ(Netstack_DeleteMapEntry(map, key1), HTTP_NDK_RESULT_OK); EXPECT_EQ(tempSize, map->size); Netstack_DestroyMap(map); } -- Gitee