Subject: LWS_UNUSED
[platform/upstream/libwebsockets.git] / test-server / test-server-http.c
index 154674b..90daaf2 100644 (file)
@@ -141,13 +141,15 @@ file_upload_cb(void *data, const char *name, const char *filename,
                        (struct per_session_data__http *)data;
        int n;
 
+       (void)n;
+
        switch (state) {
        case LWS_UFS_OPEN:
                strncpy(pss->filename, filename, sizeof(pss->filename) - 1);
                /* we get the original filename in @filename arg, but for
                 * simple demo use a fixed name so we don't have to deal with
                 * attacks  */
-               pss->post_fd = open("/tmp/post-file",
+               pss->post_fd = (lws_filefd_type)open("/tmp/post-file",
                               O_CREAT | O_TRUNC | O_RDWR, 0600);
                break;
        case LWS_UFS_FINAL_CONTENT:
@@ -159,12 +161,12 @@ file_upload_cb(void *data, const char *name, const char *filename,
                        if (pss->file_length > 100000)
                                return 1;
 
-                       n = write(pss->post_fd, buf, len);
+                       n = write((int)pss->post_fd, buf, len);
                        lwsl_notice("%s: write %d says %d\n", __func__, len, n);
                }
                if (state == LWS_UFS_CONTENT)
                        break;
-               close(pss->post_fd);
+               close((int)pss->post_fd);
                pss->post_fd = LWS_INVALID_FILE;
                break;
        }
@@ -184,7 +186,7 @@ int callback_http(struct lws *wsi, enum lws_callback_reasons reason, void *user,
        struct per_session_data__http *pss =
                        (struct per_session_data__http *)user;
        unsigned char buffer[4096 + LWS_PRE];
-       unsigned long amount, file_len, sent;
+       lws_filepos_t amount, file_len, sent;
        char leaf_path[1024];
        const char *mimetype;
        char *other_headers;
@@ -230,38 +232,37 @@ int callback_http(struct lws *wsi, enum lws_callback_reasons reason, void *user,
                        goto try_to_reuse;
                }
 
-#ifndef LWS_NO_CLIENT
+#if !defined(LWS_NO_CLIENT) && defined(LWS_OPENSSL_SUPPORT)
                if (!strncmp(in, "/proxytest", 10)) {
                        struct lws_client_connect_info i;
-                       char *rootpath = "/";
+                       char *rootpath = "/git/";
                        const char *p = (const char *)in;
 
                        if (lws_get_child(wsi))
                                break;
 
                        pss->client_finished = 0;
-                       memset(&i,0, sizeof(i));
+                       memset(&i, 0, sizeof(i));
                        i.context = lws_get_context(wsi);
-                       i.address = "git.libwebsockets.org";
-                       i.port = 80;
-                       i.ssl_connection = 0;
+                       i.address = "libwebsockets.org";
+                       i.port = 443;
+                       i.ssl_connection = 1;
                        if (p[10])
                                i.path = (char *)in + 10;
                        else
                                i.path = rootpath;
-                       i.host = "git.libwebsockets.org";
+                       i.host = i.address;
                        i.origin = NULL;
                        i.method = "GET";
                        i.parent_wsi = wsi;
-                       i.uri_replace_from = "git.libwebsockets.org/";
+                       i.uri_replace_from = "libwebsockets.org/git/";
                        i.uri_replace_to = "/proxytest/";
+
                        if (!lws_client_connect_via_info(&i)) {
                                lwsl_err("proxy connect fail\n");
                                break;
                        }
 
-
-
                        break;
                }
 #endif
@@ -292,13 +293,14 @@ int callback_http(struct lws *wsi, enum lws_callback_reasons reason, void *user,
                        p = buffer + LWS_PRE;
                        end = p + sizeof(buffer) - LWS_PRE;
 
-                       pss->fop_fd = lws_vfs_file_open(lws_get_fops(lws_get_context(wsi)),
-                                       leaf_path, &file_len, &flags);
-
+                       pss->fop_fd = lws_vfs_file_open(
+                                       lws_get_fops(lws_get_context(wsi)),
+                                       leaf_path, &flags);
                        if (!pss->fop_fd) {
                                lwsl_err("failed to open file %s\n", leaf_path);
                                return -1;
                        }
+                       file_len = lws_vfs_get_length(pss->fop_fd);
 
                        /*
                         * we will send a big jpeg file, but it could be
@@ -310,7 +312,7 @@ int callback_http(struct lws *wsi, enum lws_callback_reasons reason, void *user,
                         * depending on what connection it happens to be working
                         * on
                         */
-                       if (lws_add_http_header_status(wsi, 200, &p, end))
+                       if (lws_add_http_header_status(wsi, HTTP_STATUS_OK, &p, end))
                                return 1;
                        if (lws_add_http_header_by_token(wsi, WSI_TOKEN_HTTP_SERVER,
                                        (unsigned char *)"libwebsockets",
@@ -346,7 +348,7 @@ int callback_http(struct lws *wsi, enum lws_callback_reasons reason, void *user,
                                      p - (buffer + LWS_PRE),
                                      LWS_WRITE_HTTP_HEADERS);
                        if (n < 0) {
-                               lws_vfs_file_close(pss->fop_fd);
+                               lws_vfs_file_close(&pss->fop_fd);
                                return -1;
                        }
                        /*
@@ -420,6 +422,11 @@ int callback_http(struct lws *wsi, enum lws_callback_reasons reason, void *user,
                 */
                break;
 
+        case LWS_CALLBACK_CLIENT_RECEIVE:
+                ((char *)in)[len] = '\0';
+                lwsl_info("rx %d '%s'\n", (int)len, (char *)in);
+                break;
+
        case LWS_CALLBACK_HTTP_BODY:
                /* create the POST argument parser if not already existing */
                if (!pss->spa) {
@@ -472,7 +479,7 @@ int callback_http(struct lws *wsi, enum lws_callback_reasons reason, void *user,
                start = p;
                end = p + sizeof(buffer) - LWS_PRE;
 
-               if (lws_add_http_header_status(wsi, 200, &p, end))
+               if (lws_add_http_header_status(wsi, HTTP_STATUS_OK, &p, end))
                        return 1;
 
                if (lws_add_http_header_by_token(wsi, WSI_TOKEN_HTTP_CONTENT_TYPE,
@@ -510,8 +517,10 @@ int callback_http(struct lws *wsi, enum lws_callback_reasons reason, void *user,
                if (pss->client_finished)
                        return -1;
 
-               if (!pss->fop_fd)
+               if (!lws_get_child(wsi) && !pss->fop_fd) {
+                       lwsl_notice("fop_fd NULL\n");
                        goto try_to_reuse;
+               }
 
 #ifndef LWS_NO_CLIENT
                if (pss->reason_bf & 2) {
@@ -523,17 +532,24 @@ int callback_http(struct lws *wsi, enum lws_callback_reasons reason, void *user,
                         * suitable size to send or what's available, whichever
                         * is the smaller.
                         */
+
+
                        pss->reason_bf &= ~2;
                        wsi1 = lws_get_child(wsi);
                        if (!wsi1)
                                break;
                        if (lws_http_client_read(wsi1, &px, &lenx) < 0)
-                               goto bail;
+                               return -1;
 
                        if (pss->client_finished)
                                return -1;
+
                        break;
                }
+
+               if (lws_get_child(wsi))
+                       break;
+
 #endif
                /*
                 * we can send more of whatever it is we were sending
@@ -588,12 +604,12 @@ later:
                lws_callback_on_writable(wsi);
                break;
 penultimate:
-               lws_vfs_file_close(pss->fop_fd);
+               lws_vfs_file_close(&pss->fop_fd);
                pss->fop_fd = NULL;
                goto try_to_reuse;
 
 bail:
-               lws_vfs_file_close(pss->fop_fd);
+               lws_vfs_file_close(&pss->fop_fd);
 
                return -1;
 
@@ -614,7 +630,7 @@ bail:
                lwsl_err("LWS_CALLBACK_ESTABLISHED_CLIENT_HTTP\n");
                p = buffer + LWS_PRE;
                end = p + sizeof(buffer) - LWS_PRE;
-               if (lws_add_http_header_status(lws_get_parent(wsi), 200, &p, end))
+               if (lws_add_http_header_status(lws_get_parent(wsi), HTTP_STATUS_OK, &p, end))
                        return 1;
                if (lws_add_http_header_by_token(lws_get_parent(wsi),
                                WSI_TOKEN_HTTP_SERVER,
@@ -656,16 +672,12 @@ bail:
                assert(lws_get_parent(wsi));
                if (!lws_get_parent(wsi))
                        break;
-               // lwsl_err("LWS_CALLBACK_RECEIVE_CLIENT_HTTP: wsi %p: sock: %d, parent_wsi: %p, parent_sock:%d,  len %d\n",
-               //              wsi, lws_get_socket_fd(wsi),
-               //              lws_get_parent(wsi),
-               //              lws_get_socket_fd(lws_get_parent(wsi)), len);
                pss1 = lws_wsi_user(lws_get_parent(wsi));
                pss1->reason_bf |= 2;
                lws_callback_on_writable(lws_get_parent(wsi));
                break;
        case LWS_CALLBACK_RECEIVE_CLIENT_HTTP_READ:
-               //lwsl_err("LWS_CALLBACK_RECEIVE_CLIENT_HTTP_READ len %d\n", len);
+               //lwsl_err("LWS_CALLBACK_RECEIVE_CLIENT_HTTP_READ len %d\n", (int)len);
                assert(lws_get_parent(wsi));
                m = lws_write(lws_get_parent(wsi), (unsigned char *)in,
                                len, LWS_WRITE_HTTP);