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 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
| uint16\_t httpc\_add\_customHeader\_field(uint8\_t \*customHeader_buf, const char \*name, const char \*value) { uint16\_t len = 0;
if ((strlen((char\*) name) + strlen((char\*) value)) > DATA_BUF_SIZE) return 0;
if (strlen((char\*) customHeader_buf) == 0) { len = sprintf((char\*) customHeader_buf, "%s: %s\r\n", name, value); } else { len = sprintf((char\*) customHeader_buf, "%s%s: %s\r\n", customHeader_buf, name, value); }
return len; }
uint16\_t httpc\_send\_header(HttpRequest \*req, uint8\_t \*buf, uint8\_t \*customHeader_buf, uint16\_t content_len) { uint16\_t len;
if (httpc_isConnected == HTTPC_TRUE) { memset(buf, 0x00, DATA_BUF_SIZE);
len = sprintf((char\*) buf, "%s %s HTTP/1.1\r\n", req->method, req->uri); len += sprintf((char\*) buf + len, "Host: %s\r\n", req->host); len += sprintf((char\*) buf + len, "Connection: %s\r\n", req->connection);
// HTTP content type: POST / PUT if (content_len > 0) { len += sprintf((char\*) buf + len, "Content-Length: %d\r\n", content_len);
if (strcmp((char\*) req->content_type, HTTP_CTYPE_MULTIPART_FORM) == 0) { // HTTP content type: multipart/form-data len += sprintf((char\*) buf + len, "Content-Type: %s; boundary=%s\r\n", req->content_type, formDataBoundary); } else { // 其他HTTP Content-Type len += sprintf((char\*) buf + len, "Content-Type: %s\r\n", req->content_type); // HTTP content type: others } }
// 自定义请求头 if (customHeader_buf != NULL) { if ((strlen((char\*) customHeader_buf) + len + 2) <= DATA_BUF_SIZE) { len += sprintf((char\*) buf + len, "%s", customHeader_buf); } }
len += sprintf((char\*) buf + len, "\r\n");
#ifdef \_HTTPCLIENT\_DEBUG\_ printf(" >> HTTP Request header - Method: %s, URI: %s, Content-Length: %d\r\n", req->method, req->uri, content_len); printf("%s", buf); #endif // 发送数据 send(httpsock, buf, len); } else { len = HTTPC_FAILED; }
return len; }
uint16\_t httpc\_send\_body(uint8\_t \*buf, uint16\_t len) { uint16\_t sentlen = 0;
#ifdef \_HTTPCLIENT\_DEBUG\_ uint16\_t i; #endif
if (httpc_isConnected == HTTPC_TRUE) { do { // 发送数据 sentlen += send(httpsock, buf, len); } while (sentlen < len);
#ifdef \_HTTPCLIENT\_DEBUG\_ if(sentlen > 0) { printf(" >> HTTP Request Body\r\n"); for(i = 0; i < sentlen; i++) printf("%c", buf[i]); printf("\r\n"); } #endif } else { sentlen = HTTPC_FAILED; }
return sentlen; }
uint16\_t httpc\_send(HttpRequest \*req, uint8\_t \*buf, uint8\_t \*body, uint16\_t content_len) { uint16\_t i; uint16\_t len; uint8\_t http_header_generated = HTTPC_FAILED;
if (httpc_isConnected == HTTPC_TRUE) { do { memset(buf, 0x00, DATA_BUF_SIZE);
/\* HTTP 请求头 \*/ len = sprintf((char\*) buf, "%s %s HTTP/1.1\r\n", req->method, req->uri); len += sprintf((char\*) buf + len, "Host: %s\r\n", req->host); len += sprintf((char\*) buf + len, "Connection: %s\r\n", req->connection);
// HTTP content type: POST / PUT if (content_len > 0) { len += sprintf((char\*) buf + len, "Content-Length: %d\r\n", content_len);
if (strcmp((char\*) req->content_type, HTTP_CTYPE_MULTIPART_FORM) == 0) { // HTTP content type: multipart/form-data len += sprintf((char\*) buf + len, "Content-Type: %s; boundary=%s\r\n", req->content_type, formDataBoundary); } else { // HTTP content type: others len += sprintf((char\*) buf + len, "Content-Type: %s\r\n", req->content_type); // HTTP content type: others } } len += sprintf((char\*) buf + len, "\r\n");
// 避免缓存溢出 if ((len + content_len) > DATA_BUF_SIZE) { content_len = DATA_BUF_SIZE - len; } else { http_header_generated = HTTPC_SUCCESS; } } while (http_header_generated != HTTPC_SUCCESS);
/\* HTTP 请求 body \*/ for (i = 0; i < content_len; i++) { buf[len++] = body[i]; }
//#ifdef \_HTTPCLIENT\_DEBUG\_ printf(" >> HTTP Request - Method: %s, URI: %s, Content-Length: %d\r\n", req->method, req->uri, content_len); for (i = 0; i < len; i++) printf("%c", buf[i]); printf("\r\n"); //#endif // 发送数据 send(httpsock, buf, len); } else { len = HTTPC_FAILED; }
return len; }
uint16\_t httpc\_recv(uint8\_t \*buf, uint16\_t len) { uint16\_t recvlen;
if (httpc_isConnected == HTTPC_TRUE) { if (len > DATA_BUF_SIZE) len = DATA_BUF_SIZE; recvlen = recv(httpsock, buf, len); } else { recvlen = HTTPC_FAILED; }
return recvlen; }
|