if (strstr(pusrdata, "GET / HTTP/1.1") != NULL) //Display Config request
    {
        char * ResponseToSend = "<!DOCTYPE html><html><body><form action=\"upload\" method=\"post\" enctype=\"multipart/form-data\">Select image to upload:<input type=\"file\" name=\"fileToUpload\" id=\"fileToUpload\"><input type=\"submit\" value=\"Upload Image\" name=\"submit\"></form></body></html>";
        //espconn_send(pespconn, ResponseToSend, os_strlen(ResponseToSend));
        HttpSendWithHeader(pespconn, ResponseToSend);
    }

So Recently I coded this nice little piece for the ESP8266, It sends a Web form asking a User to upload a file to the web server, Now when it does it post the following command back

POST /upload HTTP/1.1
Host: 192.168.1.51
Connection: keep-alive
Content-Length: 1013730
Cache-Control: max-age=0
Origin: http://192.168.1.51
Upgrade-Insecure-Requests: 1
Content-Type: multipart/form-data; boundary=----WebKitFormBoundaryVD9Qk48AHQFOiDEY
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8
Referer: http://192.168.1.51/
Accept-Encoding: gzip, deflate
Accept-Language: en-US,en;q=0.9


WebServerConn_recv_callback------WebKitFormBoundaryVD9Qk48AHQFOiDEY
Content-Disposition: form-data; name="fileToUpload"; filename="Chapter 2 Data Files (1).zip"
Content-Type: application/x-zip-compressed

PK
WebServerConn_recv_callbackT�I�V�7��]%j
"�G�z�Z��M��|*�Y�       <
ʠ�%� �Ā��^����  �=wV�t�?�Jj��hA��x
                                  |���Z>        ���ϊf�4�c\�~�c�y��9��A7$
                                                                        ��:�M��������m׾O6|z�������,�Io
�W�>¼H��k)�2������O

Hex translated.

00000000  50 4f 53 54 20 2f 75 70  6c 6f 61 64 20 48 54 54   POST /up load HTT
00000010  50 2f 31 2e 31 0d 0a 48  6f 73 74 3a 20 31 39 32   P/1.1..H ost: 192
00000020  2e 31 36 38 2e 31 2e 35  31 0d 0a 43 6f 6e 6e 65   .168.1.5 1..Conne
00000030  63 74 69 6f 6e 3a 20 6b  65 65 70 2d 61 6c 69 76   ction: k eep-aliv
00000040  65 0d 0a 43 6f 6e 74 65  6e 74 2d 4c 65 6e 67 74   e..Conte nt-Lengt
00000050  68 3a 20 32 32 36 30 33  33 0d 0a 43 61 63 68 65   h: 22603 3..Cache
00000060  2d 43 6f 6e 74 72 6f 6c  3a 20 6d 61 78 2d 61 67   -Control : max-ag
00000070  65 3d 30 0d 0a 55 70 67  72 61 64 65 2d 49 6e 73   e=0..Upg rade-Ins
00000080  65 63 75 72 65 2d 52 65  71 75 65 73 74 73 3a 20   ecure-Re quests: 
00000090  31 0d 0a 55 73 65 72 2d  41 67 65 6e 74 3a 20 4d   1..User- Agent: M
000000A0  6f 7a 69 6c 6c 61 2f 35  2e 30 20 28 57 69 6e 64   ozilla/5 .0 (Wind
000000B0  6f 77 73 20 4e 54 20 31  30 2e 30 3b 20 57 69 6e   ows NT 1 0.0; Win
000000C0  36 34 3b 20 78 36 34 29  20 41 70 70 6c 65 57 65   64; x64)  AppleWe
000000D0  62 4b 69 74 2f 35 33 37  2e 33 36 20 28 4b 48 54   bKit/537 .36 (KHT
000000E0  4d 4c 2c 20 6c 69 6b 65  20 47 65 63 6b 6f 29 20   ML, like  Gecko) 
000000F0  43 68 72 6f 6d 65 2f 37  31 2e 30 2e 33 35 37 38   Chrome/7 1.0.3578
00000100  2e 39 38 20 53 61 66 61  72 69 2f 35 33 37 2e 33   .98 Safa ri/537.3
00000110  36 0d 0a 4f 72 69 67 69  6e 3a 20 68 74 74 70 3a   6..Origi n: http:
00000120  2f 2f 31 39 32 2e 31 36  38 2e 31 2e 35 31 0d 0a   //192.16 8.1.51..
00000130  43 6f 6e 74 65 6e 74 2d  54 79 70 65 3a 20 6d 75   Content- Type: mu
00000140  6c 74 69 70 61 72 74 2f  66 6f 72 6d 2d 64 61 74   ltipart/ form-dat
00000150  61 3b 20 62 6f 75 6e 64  61 72 79 3d 2d 2d 2d 2d   a; bound ary=----
00000160  57 65 62 4b 69 74 46 6f  72 6d 42 6f 75 6e 64 61   WebKitFo rmBounda
00000170  72 79 30 66 62 31 42 51  35 71 59 6e 79 46 64 68   ry0fb1BQ 5qYnyFdh
00000180  67 6e 0d 0a 41 63 63 65  70 74 3a 20 74 65 78 74   gn..Acce pt: text
00000190  2f 68 74 6d 6c 2c 61 70  70 6c 69 63 61 74 69 6f   /html,ap plicatio
000001A0  6e 2f 78 68 74 6d 6c 2b  78 6d 6c 2c 61 70 70 6c   n/xhtml+ xml,appl
000001B0  69 63 61 74 69 6f 6e 2f  78 6d 6c 3b 71 3d 30 2e   ication/ xml;q=0.
000001C0  39 2c 69 6d 61 67 65 2f  77 65 62 70 2c 69 6d 61   9,image/ webp,ima
000001D0  67 65 2f 61 70 6e 67 2c  2a 2f 2a 3b 71 3d 30 2e   ge/apng, */*;q=0.
000001E0  38 0d 0a 52 65 66 65 72  65 72 3a 20 68 74 74 70   8..Refer er: http
000001F0  3a 2f 2f 31 39 32 2e 31  36 38 2e 31 2e 35 31 2f   ://192.1 68.1.51/
00000200  0d 0a 41 63 63 65 70 74  2d 45 6e 63 6f 64 69 6e   ..Accept -Encodin
00000210  67 3a 20 67 7a 69 70 2c  20 64 65 66 6c 61 74 65   g: gzip,  deflate
00000220  0d 0a 41 63 63 65 70 74  2d 4c 61 6e 67 75 61 67   ..Accept -Languag
00000230  65 3a 20 65 6e 2d 55 53  2c 65 6e 3b 71 3d 30 2e   e: en-US ,en;q=0.
00000240  39 0d 0a 0d 0a                                     9....
00000245  2d 2d 2d 2d 2d 2d 57 65  62 4b 69 74 46 6f 72 6d   ------We bKitForm
00000255  42 6f 75 6e 64 61 72 79  30 66 62 31 42 51 35 71   Boundary 0fb1BQ5q
00000265  59 6e 79 46 64 68 67 6e  0d 0a 43 6f 6e 74 65 6e   YnyFdhgn ..Conten
00000275  74 2d 44 69 73 70 6f 73  69 74 69 6f 6e 3a 20 66   t-Dispos ition: f
00000285  6f 72 6d 2d 64 61 74 61  3b 20 6e 61 6d 65 3d 22   orm-data ; name="
00000295  66 69 6c 65 54 6f 55 70  6c 6f 61 64 22 3b 20 66   fileToUp load"; f
000002A5  69 6c 65 6e 61 6d 65 3d  22 74 65 73 74 66 6f 74   ilename= "testfot
000002B5  61 2d 30 78 30 31 30 30  30 2e 62 69 6e 22 0d 0a   a-0x0100 0.bin"..
000002C5  43 6f 6e 74 65 6e 74 2d  54 79 70 65 3a 20 61 70   Content- Type: ap
000002D5  70 6c 69 63 61 74 69 6f  6e 2f 6f 63 74 65 74 2d   plicatio n/octet-
000002E5  73 74 72 65 61 6d 0d 0a  0d 0a ea 04 00 00 04 00   stream.. ........
000002F5  10 40 00 00 00 00 80 05  03 00 90 83 fe 3f 60 84   .@...... .....?`.
00000305  fe 3f 08 0e 10 40 6b c0  3f 00 b8 83 fe 3f 08 84   .?...@k. ?....?..
00000315  fe 3f 38 08 00 60 40 10  20 40 98 0f 00 40 50 4c   .?8..`@.  @...@PL
00000325  00 40 94 19 10 40 d0 4c  00 40 12 c1 f0 09 31 f9   .@...@.L .@....1.
00000335  21 fd 01 21 f1 ff 31 f2  ff 01 f2 ff c0 00 00 1d   !..!..1. ........
00000345  0f 08 31 f8 21 12 c1 10  0d f0 12 c1 e0 09 71 c9   ..1.!... ......q.
00000355  61 f9 51 fd 01 0c 02 32  a2 b6 05 0c 00 0c 02 45   a.Q....2 .......E
00000365  6a 02 0c 02 05 6f 04 1c  02 01 ec ff c0 00 00 01   j....o.. ........
00000375  eb ff c0 00 00 0c 02 29  0f 0c 02 29 1f 0c 02 29   .......) ...)...)
00000385  2f 0c 02 29 3f 21 e0 ff  3d 0f 0c 14 01 e5 ff c0   /..)?!.. =.......
00000395  00 00 c5 5c 01 56 d2 01  05 ec 04 cd 02 45 5c 01   ...\.V.. .....E\.
000003A5  4d 02 28 0f 20 50 74 21  d8 ff 3d 0c 01 d5 ff c0   M.(. Pt! ..=.....
000003B5  00 00 c6 06 00 00 05 ea  04 20 c2 20 45 5a 01 4d   ........ . . EZ.M
000003C5  02 28 0f 20 50 74 21 d2  ff 3d 0c 01 cd ff c0 00   .(. Pt!. .=......
000003D5  00 21 d0 ff 31 cf ff c0  20 00 48 03 32 ae cf 30   .!..1...  .H.2..0
000003E5  34 10 c0 20 00 39 02 0c  42 0c 03 0c 04 0c 05 01   4.. .9.. B.......
000003F5  cd ff c0 00 00 21 c8 ff  85 08 02 1d 0f 08 71 c8   .....!.. ......q.
00000405  61 f8 51 12 c1 20 0d f0  00 00 00 0f 00 00 ff ff   a.Q.. .. ........
00000415  0f 00 00 fe ff 5f 00 00  06 00 ff ff f9 ff 61 fd   ....._.. ......a.
00000425  ff 51 fa ff 91 fa ff 20  55 d1 90 93 10 6a 55 c0   .Q.....  U....jU.
00000435  20 00 92 65 85 81 f8 ff  c0 20 00 72 25 88 80 77    ..e.... . .r%..w
00000445  20 c0 20 00 72 65 88 61  f4 ff c0 20 00 42 25 88    . .re.a ... .B%.
...
...
...
00037275  00 7c 07 03 03 00 3c 00  00 00 ff ff ff ff 80 83   .|....<. ........
00037285  fe 3f 04 02 00 00 32 2e  31 2e 30 28 31 31 36 62   .?....2. 1.0(116b
00037295  37 36 32 29 00 00 5b 25  73 5d 20 46 4f 54 41 20   762)..[% s] FOTA 
000372A5  46 49 52 4d 57 41 52 45  20 4c 4f 41 44 45 44 20   FIRMWARE  LOADED 
000372B5  41 4e 44 20 54 45 53 54  45 44 21 0d 0a 00 0d 0a   AND TEST ED!.....
000372C5  0d 0a 53 74 61 72 74 69  6e 67 20 45 53 50 38 32   ..Starti ng ESP82
000372D5  36 36 21 0d 0a 53 44 4b  20 76 65 72 73 69 6f 6e   66!..SDK  version
000372E5  3a 25 73 0d 0a 4c 6f 61  64 65 64 20 66 72 6f 6d   :%s..Loa ded from
000372F5  3a 20 30 78 25 30 32 78  0d 0a 56 64 64 33 33 5f   : 0x%02x ..Vdd33_
00037305  43 6f 6e 73 74 3a 20 25  30 32 78 0d 0a 00 0d 0a   Const: % 02x.....
00037315  0d 0a 53 74 61 72 74 69  6e 67 20 45 53 50 38 32   ..Starti ng ESP82
00037325  36 36 20 46 4f 54 41 21  0d 0a 53 44 4b 20 76 65   66 FOTA! ..SDK ve
00037335  72 73 69 6f 6e 3a 25 73  0d 0a 4c 6f 61 64 65 64   rsion:%s ..Loaded
00037345  20 66 72 6f 6d 3a 20 30  78 25 30 32 78 0d 0a 56    from: 0 x%02x..V
00037355  64 64 33 33 5f 43 6f 6e  73 74 3a 20 25 30 32 78   dd33_Con st: %02x
00037365  0d 0a 00 00 00 00 73 64  6b 5f 69 6e 69 74 5f 64   ......sd k_init_d
00037375  6f 6e 65 5f 63 62 00 00  00 00 64 00 00 00 14 08   one_cb.. ..d.....
00037385  40 00 dd 07 00 50 f2 02  00 01 00 00 00 00 ff ff   @....P.. ........
00037395  ff ff ff ff 00 00 72 69  6e 67 62 75 66 5f 66 72   ......ri ngbuf_fr
000373A5  65 65 00 00 00 00 72 69  6e 67 62 75 66 5f 6e 65   ee....ri ngbuf_ne
000373B5  78 74 70 00 00 00 72 69  6e 67 62 75 66 5f 66 69   xtp...ri ngbuf_fi
000373C5  6e 64 63 68 72 00 72 69  6e 67 62 75 66 5f 6d 65   ndchr.ri ngbuf_me
000373D5  6d 73 65 74 00 00 72 69  6e 67 62 75 66 5f 6d 65   mset..ri ngbuf_me
000373E5  6d 63 70 79 5f 69 6e 74  6f 00 00 00 00 00 00 00   mcpy_int o.......
000373F5  00 00 00 00 00 00 72 69  6e 67 62 75 66 5f 6d 65   ......ri ngbuf_me
00037405  6d 63 70 79 5f 66 72 6f  6d 00 04 08 10 0c 04 08   mcpy_fro m.......
00037415  10 0c 00 00 20 42 00 00  80 43 00 00 00 00 00 00   .... B.. .C......
00037425  20 41 25 73 20 25 75 0a  00 00 6d 61 69 00 74 69    A%s %u. ..mai.ti
00037435  6d 00 45 3a 4d 20 25 64  0a 00 25 78 20 61 6c 72   m.E:M %d ..%x alr
00037445  65 61 64 79 20 66 72 65  65 64 0a 00 00 00 2c 01   eady fre ed....,.
00037455  00 00 18 fe 34 00 ff ff  ff ff ff ff 00 00 25 30   ....4... ......%0
00037465  32 58 00 00 00 00 25 30  32 78 00 00 00 00 6d 61   2X....%0 2x....ma
00037475  63 00 70 6d 00 00 66 70  6d 00 70 70 00 00 64 65   c.pm..fp m.pp..de
00037485  76 00 88 85 fe 3f 50 e8  fe 3f 00 00 00 00 00 00   v....?P. .?......
00037495  00 00 00 00 00 00 00 00  00 a5 0d 0a 2d 2d 2d 2d   ........ ....----
000374A5  2d 2d 57 65 62 4b 69 74  46 6f 72 6d 42 6f 75 6e   --WebKit FormBoun
000374B5  64 61 72 79 30 66 62 31  42 51 35 71 59 6e 79 46   dary0fb1 BQ5qYnyF
000374C5  64 68 67 6e 0d 0a 43 6f  6e 74 65 6e 74 2d 44 69   dhgn..Co ntent-Di
000374D5  73 70 6f 73 69 74 69 6f  6e 3a 20 66 6f 72 6d 2d   spositio n: form-
000374E5  64 61 74 61 3b 20 6e 61  6d 65 3d 22 73 75 62 6d   data; na me="subm
000374F5  69 74 22 0d 0a 0d 0a 55  70 6c 6f 61 64 20 49 6d   it"....U pload Im
00037505  61 67 65 0d 0a 2d 2d 2d  2d 2d 2d 57 65 62 4b 69   age..--- ---WebKi
00037515  74 46 6f 72 6d 42 6f 75  6e 64 61 72 79 30 66 62   tFormBou ndary0fb
00037525  31 42 51 35 71 59 6e 79  46 64 68 67 6e 2d 2d 0d   1BQ5qYny Fdhgn--.
00037535  0a                                                 .

We have to parse the data as it comes in

if (RemotePortsOpenedInDownload == pespconn->proto.tcp->remote_port)
    {
        os_printf("Incoming file data gcontentlength = %d\r\n", gContentLength);
        //os_printf(pusrdata);
        char * Ptr = pusrdata;
        if (HeaderRead == false)
        {
            while (!strstarts(Ptr, "\r\n\r\n"))
            {
                Ptr++; //Increment Ptr
                //lengthleft--;
            }
            while (*Ptr == '\n' || *Ptr == '\r')
            {
                Ptr++; //Increment Ptr
                //lengthleft--;
            }
            HeaderRead = true;
        }
        //os_printf("---");
        //os_printf(Ptr);
        os_printf("gcontentlength(%d) - length(%d) = %d\r\n", length, gContentLength, (gContentLength - lengthleft));
        gContentLength -= lengthleft;
        if (gContentLength == 0)
        {
            //espconn_disconnect(&WebServerSocket); //Webservers don't close sockets, clients do
            char * ResponseToSend = "<!DOCTYPE html><html><body>File Uploaded</body></html>";
            HttpSendWithHeader(pespconn, ResponseToSend);

            uint8 id = system_upgrade_userbin_check();
            char *next = id ? "user1.bin" : "user2.bin";
            os_printf("OTA: Next firmware: %s (%d=0x%08x)\n", next, id, system_get_userbin_addr());

            // Information about flash size based on flash memory map as returned by system_get_flash_size_map:
            // 4Mb_256, 2Mb, 8Mb_512, 16Mb_512, 32Mb_512, 16Mb_1024, 32Mb_1024
            static enum flash_size_map flashSizeMap;

            // Start address of user2.bin (address of user1 is always 0x1000)
            static uint32_t flashUser2Addr[] = {
              260*1024, 0, 516*1024, 516*1024, 516*1024, 1028*1024, 1028*1024,
            };

            uint32_t offset;
            // let's see which what flash address we're uploading to
            uint32_t address = id ? 0x1000 : flashUser2Addr[flashSizeMap];
            address += offset;

            // erase next flash block if necessary
            if (address % SPI_FLASH_SEC_SIZE == 0)
            {
                os_printf("OTA Flashing 0x%05lx\n", (long unsigned int) address);
                spi_flash_erase_sector(address/SPI_FLASH_SEC_SIZE);
            }
        }
    }

    if (strstr(pusrdata, "POST /upload") != NULL)
    {
        RemotePortsOpenedInDownload = pespconn->proto.tcp->remote_port;
        //os_printf(pusrdata);
        int index = 0;
        char * Ptr = pusrdata;

        while (!strstarts(Ptr, "Content-Length: "))
        {
            Ptr++; //Increment Potr
        }
        while (*Ptr != ' ')
        {
            Ptr++; //Increment Potr
        }
        Ptr++; //Increment to Content String

        char ContentLength[8] = {0};
        index = 0;
        while (*Ptr != '\r' && *Ptr != '\n')
        {
            ContentLength[index] = *Ptr;
            index++;
            Ptr++; //Increment Potr
        }
        os_printf(ContentLength);
        gContentLength = atoi(ContentLength);

        while (!strstarts(Ptr, "Content-Type: multipart/form-data; boundary="))
        {
            Ptr++; //Increment Potr
        }
        while (*Ptr != '=')
        {
            Ptr++; //Increment Potr
        }
        Ptr++; //Increment to Content String
        char Boundary[64] = {0};        
        index = 0;

        while (*Ptr != '\r' && *Ptr != '\n')
        {
            Boundary[index] = *Ptr;
            index++;
            Ptr++; //Increment Potr
        }
        os_printf(Boundary);
        os_printf("\r\n");

        HeaderRead = false; //SetHeader back to false to prep for incoming header to skil

    }

Leave a Reply

Your email address will not be published. Required fields are marked *

To create code blocks or other preformatted text, indent by four spaces:

    This will be displayed in a monospaced font. The first four 
    spaces will be stripped off, but all other whitespace
    will be preserved.
    
    Markdown is turned off in code blocks:
     [This is not a link](http://example.com)

To create not a block, but an inline code span, use backticks:

Here is some inline `code`.

For more help see http://daringfireball.net/projects/markdown/syntax