rjw | 1f88458 | 2022-01-06 17:20:42 +0800 | [diff] [blame^] | 1 | #ifndef _LYNQ_HTTP_H_ |
| 2 | #define _LYNQ_HTTP_H_ |
| 3 | |
| 4 | #define HTTP_API |
| 5 | |
| 6 | |
| 7 | #include "lynq_http_parser.h" |
| 8 | #include "lynq_msgq.h" |
| 9 | #include <stdlib.h> |
| 10 | #include <string.h> |
| 11 | #include <stdio.h> |
| 12 | #include <pthread.h> |
| 13 | #include <sys/socket.h> |
| 14 | #include <netinet/in.h> |
| 15 | #include <arpa/inet.h> |
| 16 | #include <fcntl.h> |
| 17 | #include <stdio.h> |
| 18 | #include <stdlib.h> |
| 19 | #include <unistd.h> |
| 20 | #include <errno.h> |
| 21 | #include <sys/types.h> |
| 22 | #include <sys/select.h> |
| 23 | #include <time.h> |
| 24 | #include <ctype.h> |
| 25 | #include <netdb.h> |
| 26 | #include <strings.h> |
| 27 | #include <openssl/ssl.h> |
| 28 | #include <openssl/err.h> |
| 29 | |
| 30 | #ifdef _cplusplus |
| 31 | extern "C" { |
| 32 | #endif |
| 33 | |
| 34 | #define HTTP_INVALID_SOCKET -1 |
| 35 | #define HTTP_EINTR EINTR |
| 36 | #define HTTP_EINPROGRESS EINPROGRESS |
| 37 | #define HTTP_EWOULDBLOCK EWOULDBLOCK |
| 38 | #define HTTP_EALREADY EALREADY |
| 39 | |
| 40 | #define free_member(member) if((member)) { free(member); (member) = NULL; } |
| 41 | #define close_socket(fd) if(fd != HTTP_INVALID_SOCKET) { socket_close(fd); fd = HTTP_INVALID_SOCKET; } |
| 42 | #define close_file(pf) if(pf != NULL) { fclose(pf); pf = NULL; } |
| 43 | |
| 44 | #define RECV_BUF_SIZE 4 * 1024 |
| 45 | |
| 46 | #define socket_close close |
| 47 | |
| 48 | |
| 49 | enum parser_statue_e { PARSERD_NONE = 0, PARSERD_FIELD, PARSERD_VALUE, PARSERD_BODY }; |
| 50 | |
| 51 | enum proto_type_e { PROTO_HTTP = 0, PROTO_HTTPS }; |
| 52 | typedef enum http_request_method_e |
| 53 | { |
| 54 | M_GET = 0, |
| 55 | M_POST, |
| 56 | M_HEAD |
| 57 | } http_request_method_e; |
| 58 | |
| 59 | typedef enum http_connent_method_e |
| 60 | { |
| 61 | M_CLOSE = 0, |
| 62 | M_KEEP, |
| 63 | } http_connent_method_e; |
| 64 | |
| 65 | enum http_error_e |
| 66 | { |
| 67 | ERR_OK = 0, |
| 68 | |
| 69 | ERR_INVALID_PARAM, |
| 70 | ERR_OUT_MEMORY, |
| 71 | ERR_OPEN_FILE, |
| 72 | ERR_PARSE_REP, |
| 73 | ERR_URL_INVALID, |
| 74 | ERR_URL_INVALID_PROTO, |
| 75 | ERR_URL_INVALID_HOST, |
| 76 | ERR_URL_INVALID_IP, |
| 77 | ERR_URL_RESOLVED_HOST, |
| 78 | ERR_SOCKET_CREATE = 10, |
| 79 | ERR_SOCKET_SET_OPT, |
| 80 | ERR_SOCKET_NOBLOCKING, |
| 81 | ERR_SOCKET_CONNECT, |
| 82 | ERR_SOCKET_CONNECT_TIMEOUT, |
| 83 | ERR_SOCKET_SELECT, |
| 84 | ERR_SOCKET_WRITE, |
| 85 | ERR_SOCKET_READ, |
| 86 | ERR_SOCKET_TIMEOUT, |
| 87 | ERR_SOCKET_CLOSED, |
| 88 | ERR_SOCKET_GET_OPT = 20, |
| 89 | ERR_SSL_CREATE_CTX, |
| 90 | ERR_SSL_CREATE_SSL, |
| 91 | ERR_SSL_SET_FD, |
| 92 | ERR_SSL_CONNECT, |
| 93 | ERR_SSL_WRITE, |
| 94 | ERR_SSL_READ, |
| 95 | ERR_NOCERT, |
| 96 | ERR_MSG |
| 97 | }; |
| 98 | |
| 99 | struct lynq_http_client_t; |
| 100 | typedef struct lynq_http_client_t lynq_http_client_t; |
| 101 | typedef int (*data_recv_cb_t)( lynq_http_client_t* http, const char* data, int size, int total, void* user); |
| 102 | |
| 103 | typedef int socket_t; |
| 104 | |
| 105 | |
| 106 | struct lynq_http_client_t |
| 107 | { |
| 108 | int index; |
| 109 | |
| 110 | char protocol[10]; |
| 111 | int session; |
| 112 | |
| 113 | char action[10]; |
| 114 | char *data; |
| 115 | char *section; |
| 116 | int request_method; |
| 117 | int connent_method; |
| 118 | int sockfd; |
| 119 | int modify_thread; |
| 120 | int add_thread; |
| 121 | |
| 122 | FILE* pf; |
| 123 | char* filename; |
| 124 | char* body; |
| 125 | char* redirect_url; |
| 126 | char* header_field; |
| 127 | char* header_value; |
| 128 | |
| 129 | char* post_data; |
| 130 | char* url; |
| 131 | int post_data_len; |
| 132 | struct http_parser_url u; |
| 133 | char* user_header; |
| 134 | int user_header_len; |
| 135 | |
| 136 | http_parser_settings parser_setting; |
| 137 | struct http_parser parser; |
| 138 | |
| 139 | |
| 140 | char* user; |
| 141 | data_recv_cb_t recv_cb; |
| 142 | |
| 143 | unsigned long body_len; |
| 144 | unsigned long content_length; |
| 145 | |
| 146 | enum http_request_method_e method; |
| 147 | enum http_connent_method_e conn_method; |
| 148 | enum proto_type_e proto_type; |
| 149 | |
| 150 | unsigned short field_size; |
| 151 | unsigned short value_size; |
| 152 | unsigned short cur_field_size; |
| 153 | unsigned short cur_value_size; |
| 154 | SSL_CTX *ctx; |
| 155 | SSL *ssl; |
| 156 | |
| 157 | socket_t fd; |
| 158 | int timeout; |
| 159 | |
| 160 | short status_code; |
| 161 | char parser_statue; |
| 162 | char error_code; |
| 163 | unsigned cancel : 1; |
| 164 | unsigned exit : 1; |
| 165 | unsigned download : 1; |
| 166 | unsigned redirect : 1; |
| 167 | }; |
| 168 | |
| 169 | |
| 170 | int lynq_http_init(void); |
| 171 | lynq_http_client_t* lynq_http_new(); |
| 172 | void lynq_http_destroy(lynq_http_client_t* http); |
| 173 | int lynq_http_get_error_code(lynq_http_client_t* http); |
| 174 | const char* lynq_http_sync_request(lynq_http_client_t* http, const char* url, http_request_method_e m, http_connent_method_e c_m); |
| 175 | const char* lynq_http_sync_post_request(lynq_http_client_t* http, char* url, char* post_data, http_request_method_e m, http_connent_method_e c_m); |
| 176 | int lynq_http_sync_download_file(lynq_http_client_t* http, char* url, char* filepath, http_request_method_e m, http_connent_method_e c_m); |
| 177 | int lynq_http_set_data_recv_cb(lynq_http_client_t* http, data_recv_cb_t cb, void* user); |
| 178 | int lynq_http_exit(lynq_http_client_t* http); |
| 179 | int lynq_http_data_send(char *data); |
| 180 | void *lynq_http_write_head_data(lynq_http_client_t* http); |
| 181 | #ifdef _cplusplus |
| 182 | } |
| 183 | #endif |
| 184 | #endif |
| 185 | |