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