forked from swoole/swoole-src
-
Notifications
You must be signed in to change notification settings - Fork 0
/
swoole_http_client.h
72 lines (62 loc) · 2.42 KB
/
swoole_http_client.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/*
+----------------------------------------------------------------------+
| Swoole |
+----------------------------------------------------------------------+
| This source file is subject to version 2.0 of the Apache license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.apache.org/licenses/LICENSE-2.0.html |
| If you did not receive a copy of the Apache2.0 license and are unable|
| to obtain it through the world-wide-web, please send a note to |
| [email protected] so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Author: Tianfeng Han <[email protected]> |
+----------------------------------------------------------------------+
*/
#pragma once
#include "swoole_http.h"
#ifdef __cplusplus
extern "C" {
#endif
#include "websocket.h"
#include "thirdparty/swoole_http_parser.h"
#include "ext/standard/basic_functions.h"
#include "ext/standard/php_http.h"
#include "ext/standard/base64.h"
#ifdef SW_HAVE_ZLIB
#include <zlib.h>
#endif
#ifdef __cplusplus
}
#endif
enum http_client_error_status_code
{
HTTP_CLIENT_ESTATUS_CONNECT_FAILED = -1,
HTTP_CLIENT_ESTATUS_REQUEST_TIMEOUT = -2,
HTTP_CLIENT_ESTATUS_SERVER_RESET = -3,
HTTP_CLIENT_ESTATUS_SEND_FAILED = -4,
};
static sw_inline void http_client_create_token(int length, char *buf)
{
char characters[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!\"§$%&/()=[]{}";
int i;
assert(length < 1024);
for (i = 0; i < length; i++)
{
buf[i] = characters[rand() % (sizeof(characters) - 1)];
}
buf[length] = '\0';
}
static sw_inline void http_client_swString_append_headers(swString* swStr, const char* key, size_t key_len, const char* data, size_t data_len)
{
swString_append_ptr(swStr, key, key_len);
swString_append_ptr(swStr, ZEND_STRL(": "));
swString_append_ptr(swStr, data, data_len);
swString_append_ptr(swStr, ZEND_STRL("\r\n"));
}
static sw_inline void http_client_append_content_length(swString* buf, int length)
{
char content_length_str[32];
int n = snprintf(SW_STRS(content_length_str), "Content-Length: %d\r\n\r\n", length);
swString_append_ptr(buf, content_length_str, n);
}