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