blob: 657b838a0254d19dd14d153f74be7f6c16b2220e [file] [log] [blame]
liubin281ac462023-07-19 14:22:54 +08001#include <sys/epoll.h>
2#include <string.h>
3
4#include "mbtk_log.h"
5#include "mbtk_http_base.h"
6
7static void http_sock_cb_func(int handle, int fd, int event);
8
liubin281ac462023-07-19 14:22:54 +08009static bool http_sock_inited = FALSE;
10static int http_handle = -1;
11static int http_fd = -1;
12
13static void http_sock_cb_func(int handle, int fd, int event)
14{
15 if(http_handle == handle && http_fd == fd) {
16 if(event & EPOLLIN) { // READ
17
18 } else if(event & EPOLLRDHUP) { // Close
19
20 } else {
21 LOGW("Unknown event:%x",event);
22 }
23 }
24}
25
26int mbtk_http_init()
27{
28 if(http_sock_inited) {
29 LOGE("HTTP has inited.");
30 return -1;
31 }
32
b.liueb040652023-09-25 18:50:56 +080033 mbtk_init_info init_info;
34 memset(&init_info, 0x0, sizeof(mbtk_init_info));
35 init_info.net_type = MBTK_NET_LINUX;
36 init_info.sock_cb = http_sock_cb_func;
37 http_handle = mbtk_sock_init(&init_info);
liubin281ac462023-07-19 14:22:54 +080038 if(http_handle < 0) {
39 LOGE("mbtk_sock_init() fail.");
40 return -1;
41 }
42
43 http_sock_inited = TRUE;
44 return 0;
45}
46
47int mbtk_http_deinit()
48{
49 if(!http_sock_inited) {
50 LOGE("HTTP not inited.");
51 return -1;
52 }
53
54 int err = mbtk_sock_deinit(http_handle);
55 if(err != MBTK_SOCK_SUCCESS) {
56 LOGE("mbtk_sock_deinit() fail.");
57 return -1;
58 }
59
60 http_handle = -1;
61 http_sock_inited = FALSE;
62 return 0;
63}
64
65
66int mbtk_http_open
67(
68 bool is_ssl,
69 bool ingnore_cert,
70 const void *host,
71 uint16 port
72)
73{
74 int err;
75 mbtk_sock_info sock_info;
76 memset(&sock_info,0x0,sizeof(mbtk_sock_info));
77
78 sock_info.type = MBTK_SOCK_TCP;
79 sock_info.is_support_ssl = is_ssl;
80 sock_info.ingnore_cert = ingnore_cert;
81 memcpy(sock_info.address,host,strlen(host));
82 sock_info.port = port;
83
84 http_fd = mbtk_sock_open(http_handle,&sock_info, 3000, &err);
85
86 return http_fd;
87}
88
89/*=============================================
90FUNCTION
91 mbtk_http_read
92
93DESCRIPTION
94 read content from socket.
95
96DEPENDENCIES
97 None
98
99PARAMETERS
100 *buf Store read content.
101 len the length of Content.
102 timeout Set timeout
103
104RETURN VALUE
105 Length of read content
106
107SIDE EFFECTS
108 None
109=============================================*/
110int mbtk_http_read
111(
112 int sock_fd,
113 void *buf,
114 uint16 len,
115 int timeout_ms
116)
117{
118 int err;
119 int read_len = mbtk_sock_read(http_handle, sock_fd, buf, len, timeout_ms, &err);
120 if(read_len < 0) {
121 if(err == MBTK_SOCK_ETIMEOUT) {
122 return -2;
123 } else {
124 return -1;
125 }
126 } else {
127 return read_len;
128 }
129}
130
131#if 0
132int mbtk_http_read_line
133(
134 FILE *file,
135 void *buf,
136 uint16 len
137)
138{
139 if(file) {
140 char *buf_ptr = (char*)buf;
141 char *line = NULL;
142read_again:
143 line = fgets(buf_ptr,len,file);
144 if(!line && errno == EWOULDBLOCK) {
145 usleep(100000);
146 goto read_again;
147 }
148 if(line && strlen(line) > 0
149 && strlen(line) <= len
150 && buf_ptr[strlen(line) - 1] == '\n') {
151 LOGV("Read-Line[%d]:%s",strlen(line),line);
152 return strlen(line);
153 }else{
154 LOGE("fgets() fail.");
155 return -1;
156 }
157 }
158
159 return -1;
160}
161#else
162int mbtk_http_read_line
163(
164 int sock_fd,
165 void *buf,
166 uint16 len
167)
168{
169#if 1
170 if(sock_fd > 0) {
171 char *buf_ptr = (char*)buf;
172 char read_buf[1];
173 int read_len = 0;
174 while(TRUE) {
b.liueb040652023-09-25 18:50:56 +0800175 if(mbtk_sock_read_sync(http_handle, sock_fd, read_buf, 1) == 1) {
liubin281ac462023-07-19 14:22:54 +0800176 *buf_ptr++ = read_buf[0];
177 read_len++;
178
179 if(read_buf[0] == '\n' || read_len >= len) {
180 return read_len;
181 }
182 } else {
183 return -1;
184 }
185 }
186 }
187#else
188 if(http_handle >= 0) {
189 char *buf_ptr = (char*)buf;
190 char read_buf[1];
191 int read_len = 0;
192 while(TRUE) {
193 if(read(http_fd, read_buf, 1) == 1) {
194 *buf_ptr++ = read_buf[0];
195 read_len++;
196
197 if(read_buf[0] == '\n' || read_len >= len) {
198 return read_len;
199 }
200 } else {
201 if(errno == EWOULDBLOCK) {
202 usleep(100000);
203 }
204 }
205 }
206 }
207
208#endif
209
210 return -1;
211}
212
213#endif
214
215/*=============================================
216FUNCTION
217 mbtk_http_write
218
219DESCRIPTION
220 Write content to socket.
221
222DEPENDENCIES
223 None
224
225PARAMETERS
226 *buf Content to be transferred
227 len the length of Content.
228
229RETURN VALUE
230 Length of written content
231
232SIDE EFFECTS
233 None
234=============================================*/
235int mbtk_http_write
236(
237 int sock_fd,
238 void *buf,
239 uint16 len
240)
241{
242 int err;
243 LOGV("Write[%d]:%s",len,(char*)buf);
244 return mbtk_sock_write(http_handle, sock_fd, buf, len, 300, &err);
245}
246
247/*=============================================
248FUNCTION
249 mbtk_http_close
250
251DESCRIPTION
252 close HTTP service.
253
254DEPENDENCIES
255 None
256
257PARAMETERS
258 *err Error number
259
260RETURN VALUE
261 TURE or FALSE
262
263SIDE EFFECTS
264 None
265=============================================*/
266int mbtk_http_close(int sock_fd)
267{
268 int err;
269
270 if(mbtk_sock_close(http_handle, sock_fd,1000, &err)) {
271 return -1;
272 }
273
274 sock_fd = -1;
275 return 0;
276}
277