Explorar el Código

[update] 添加可以识别部分服务器返回 header 字段中的"Content-Type" 为小写的"content-type"的功能函数。

liangyingjian hace 6 años
padre
commit
546f62c5fc
Se han modificado 1 ficheros con 65 adiciones y 29 borrados
  1. 65 29
      src/webclient.c

+ 65 - 29
src/webclient.c

@@ -13,7 +13,6 @@
  * 2018-07-26     chenyong     modify log information
  * 2018-08-07     chenyong     modify header processing
  */
-
 #include <stdio.h>
 #include <stdlib.h>
 
@@ -46,6 +45,45 @@
 
 extern long int strtol(const char *nptr, char **endptr, int base);
 
+static int _tolower(int c)
+{
+    if (c >= 'A' && c <= 'Z')
+        c ^= 0x20;
+    return c;
+}
+
+static int webclient_strncasecmp(const char *a, const char *b, size_t n)
+{
+    uint8_t c1, c2;
+    if (n <= 0)
+        return 0;
+    do {
+        c1 = _tolower(*a++);
+        c2 = _tolower(*b++);
+    } while (--n && c1 && c1 == c2);
+    return c1 - c2;
+}
+
+static const char *webclient_strstri(const char* str, const char* subStr)
+{
+    int len = strlen(subStr);
+
+    if(len == 0)
+    {
+        return RT_NULL;
+    }
+
+    while(*str)
+    {
+        if(webclient_strncasecmp(str, subStr, len) == 0)
+        {
+            return str;
+        }
+        ++str;
+    }
+    return RT_NULL;
+}
+
 static int webclient_send(struct webclient_session* session, const unsigned char *buffer, size_t len, int flag)
 {
 #ifdef WEBCLIENT_USING_MBED_TLS
@@ -65,7 +103,7 @@ static int webclient_recv(struct webclient_session* session, unsigned char *buff
     {
         return mbedtls_client_read(session->tls_session, buffer, len);
     }
-#endif
+#endif 
 
     return recv(session->socket, buffer, len, flag);
 }
@@ -86,8 +124,8 @@ static int webclient_read_line(struct webclient_session *session, char *buffer,
         if (session->is_tls && (rc == MBEDTLS_ERR_SSL_WANT_READ || rc == MBEDTLS_ERR_SSL_WANT_WRITE))
         {
             continue;
-        }
-#endif
+        }    
+#endif 
         if (rc <= 0)
             return rc;
 
@@ -195,7 +233,7 @@ static int webclient_resolve_address(struct webclient_session *session, struct a
         }
         host_addr_len = ptr - host_addr;
         *request = (char *) ptr;
-
+        
         /* resolve port */
         port_ptr = rt_strstr(host_addr, ":");
         if (port_ptr && port_ptr < ptr)
@@ -241,7 +279,7 @@ static int webclient_resolve_address(struct webclient_session *session, struct a
         {
             return -WEBCLIENT_NOMEM;
         }
-
+        
         return rc;
     }
 #endif
@@ -356,7 +394,7 @@ static int webclient_connect(struct webclient_session *session, const char *URI)
         {
             LOG_E("connect failed, https client open URI(%s) failed!", URI);
             return -WEBCLIENT_ERROR;
-        }
+        } 
         session->is_tls = RT_TRUE;
 #else
         LOG_E("not support https connect, please enable webclient https configure!");
@@ -530,7 +568,7 @@ const char *webclient_header_fields_get(struct webclient_session *session, const
     resp_buf = session->header->buffer;
     while (resp_buf_len < session->header->length)
     {
-        if (rt_strstr(resp_buf, fields))
+        if (webclient_strstri(resp_buf, fields))
         {
             char *mime_ptr = RT_NULL;
 
@@ -556,7 +594,6 @@ const char *webclient_header_fields_get(struct webclient_session *session, const
 
     return RT_NULL;
 }
-
 /**
  * get http response status code.
  *
@@ -673,17 +710,17 @@ static int webclient_send_header(struct webclient_session *session, int method)
             webclient_write(session, (unsigned char *) session->header->buffer, session->header->length);
         }
     }
-
+    
     /* get and echo request header data */
     {
         char *header_str, *header_ptr;
         int header_line_len;
         LOG_D("request header:");
-
+        
         for(header_str = session->header->buffer; (header_ptr = rt_strstr(header_str, "\r\n")) != RT_NULL; )
         {
             header_line_len = header_ptr - header_str;
-
+            
             if (header_line_len > 0)
             {
                 LOG_D("%.*s", header_line_len, header_str);
@@ -694,7 +731,7 @@ static int webclient_send_header(struct webclient_session *session, int method)
         LOG_RAW("\n");
 #endif
     }
-
+    
 __exit:
     return rc;
 }
@@ -746,7 +783,7 @@ int webclient_handle_response(struct webclient_session *session)
 
         /* echo response header data */
         LOG_D("%s", mime_buffer);
-
+        
         session->header->length += rc;
 
         if (session->header->length >= session->header->size)
@@ -755,7 +792,6 @@ int webclient_handle_response(struct webclient_session *session)
             return -WEBCLIENT_NOMEM;
         }
     }
-
     /* get HTTP status code */
     mime_ptr = web_strdup(session->header->buffer);
     if (mime_ptr == RT_NULL)
@@ -893,7 +929,7 @@ int webclient_get(struct webclient_session *session, const char *URI)
     /* handle the response header of webclient server */
     resp_status = webclient_handle_response(session);
 
-    LOG_D("get position handle response(%d).", resp_status);
+    LOG_D("get position handle response(%d).", resp_status); 
 
     if (resp_status > 0)
     {
@@ -970,7 +1006,7 @@ int webclient_get_position(struct webclient_session *session, const char *URI, i
     /* handle the response header of webclient server */
     resp_status = webclient_handle_response(session);
 
-    LOG_D("get position handle response(%d).", resp_status);
+    LOG_D("get position handle response(%d).", resp_status); 
 
     if (resp_status > 0)
     {
@@ -1049,7 +1085,7 @@ int webclient_post(struct webclient_session *session, const char *URI, const cha
 
         /* resolve response data, get http status code */
         resp_status = webclient_handle_response(session);
-        LOG_D("post handle response(%d).", resp_status);
+        LOG_D("post handle response(%d).", resp_status); 
     }
 
     return resp_status;
@@ -1219,13 +1255,13 @@ int webclient_read(struct webclient_session *session, unsigned char *buffer, siz
         if (bytes_read <= 0)
         {
 #if defined(WEBCLIENT_USING_SAL_TLS) || defined(WEBCLIENT_USING_MBED_TLS)
-            if(session->is_tls &&
+            if(session->is_tls && 
                 (bytes_read == MBEDTLS_ERR_SSL_WANT_READ || bytes_read == MBEDTLS_ERR_SSL_WANT_WRITE))
             {
                 continue;
             }
-
-#endif
+                
+#endif  
             LOG_D("receive data error(%d).", bytes_read);
 
             if (total_read)
@@ -1298,7 +1334,7 @@ int webclient_write(struct webclient_session *session, const unsigned char *buff
         if (bytes_write <= 0)
         {
 #if defined(WEBCLIENT_USING_SAL_TLS) || defined(WEBCLIENT_USING_MBED_TLS)
-            if(session->is_tls &&
+            if(session->is_tls && 
                 (bytes_write == MBEDTLS_ERR_SSL_WANT_READ || bytes_write == MBEDTLS_ERR_SSL_WANT_WRITE))
             {
                 continue;
@@ -1346,7 +1382,7 @@ int webclient_write(struct webclient_session *session, const unsigned char *buff
 int webclient_close(struct webclient_session *session)
 {
     RT_ASSERT(session);
-
+    
 #ifdef WEBCLIENT_USING_MBED_TLS
     if (session->tls_session)
     {
@@ -1356,7 +1392,7 @@ int webclient_close(struct webclient_session *session)
     {
         if (session->socket >= 0)
         {
-            closesocket(session->socket);
+            closesocket(session->socket); 
             session->socket = -1;
         }
     }
@@ -1485,7 +1521,7 @@ int webclient_response(struct webclient_session *session, unsigned char **respon
 }
 
 /**
- * add request(GET/POST) header data.
+ * add request(GET/POST) header data. 
  *
  * @param request_header add request buffer address
  * @param fmt fields format
@@ -1554,7 +1590,7 @@ int webclient_request_header_add(char **request_header, const char *fmt, ...)
  */
 int webclient_request(const char *URI, const char *header, const char *post_data, unsigned char **response)
 {
-    struct webclient_session *session;
+    struct webclient_session *session = RT_NULL;
     int rc = WEBCLIENT_OK;
     int totle_length = 0;
 
@@ -1583,7 +1619,7 @@ int webclient_request(const char *URI, const char *header, const char *post_data
 
             for(header_str = (char *)header; (header_ptr = rt_strstr(header_str, "\r\n")) != RT_NULL; )
             {
-                header_line_length = header_ptr + rt_strlen("\r\n") - header_str;
+                header_line_length = header_ptr + rt_strlen("\r\n") - header_str; 
                 webclient_header_fields_add(session, "%.*s", header_line_length, header_str);
                 header_str += header_line_length;
             }
@@ -1619,7 +1655,7 @@ int webclient_request(const char *URI, const char *header, const char *post_data
 
             for(header_str = (char *)header; (header_ptr = rt_strstr(header_str, "\r\n")) != RT_NULL; )
             {
-                header_line_length = header_ptr + rt_strlen("\r\n") - header_str;
+                header_line_length = header_ptr + rt_strlen("\r\n") - header_str; 
                 webclient_header_fields_add(session, "%.*s", header_line_length, header_str);
                 header_str += header_line_length;
             }
@@ -1640,7 +1676,7 @@ int webclient_request(const char *URI, const char *header, const char *post_data
             rc = -WEBCLIENT_ERROR;
             goto __exit;
         }
-
+        
         totle_length = webclient_response(session, response);
         if (totle_length <= 0)
         {