Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. [C/C++] Parse multipart/form-data POST request in a small web server

[C/C++] Parse multipart/form-data POST request in a small web server

Scheduled Pinned Locked Moved C / C++ / MFC
questionc++htmlsysadminagentic-ai
2 Posts 1 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    csrss
    wrote on last edited by
    #1

    Hi there. I am looking for a simple, basic, preferable a C way of parsing multipart/form-data POST request from a client browser. So far I have a really simple web server, which reads web browser requests in a loop and prints them to to command prompt window:

    do
    {
    	len = recv(..., input\_buf, ...); 
    
    } 
        while (!strstr(input\_buf, "\\r\\n\\r\\n") && len > 0); 
    
    // Try to parse header and data
    char \* pch = nullptr, \*context = nullptr;
    pch = strtok\_s(input\_buf, "\\r\\n", &context);
    while (pch != nullptr)
    {
    	::puts(pch);
    	pch = strtok\_s (nullptr, "\\r\\n", &context);
    }
    

    When this web server starts, it just shows the following web form to the web browser:

    <form type=multipart/form-data method=post action=submit><input type=file multiple required /><input type=submit /></form>

    So far so good, ok, I connect to my server with firefox, select some test text file, which is a really small one - just a couple of bytes and hit an html form submit button. Server reads request and I have the following output in command prompt window:

    POST /submit HTTP/1.1
    Host: localhost:6666
    User-Agent: Mozilla/5.0 (Windows NT 6.2; WOW64; rv:24.0) Gecko/20100101 Firefox/
    24.0
    Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
    Accept-Language: en-US,en;q=0.5
    Accept-Encoding: gzip, deflate
    Referer: http://localhost:6666/
    Connection: keep-alive
    Content-Type: application/x-www-form-urlencoded
    Content-Length: 0

    But the question is, how do I get file data? I was reading tons of online docs, when it says something about boundary strings - I have no idea what are boundary strings. I am just trying to figure out how to grab raw bytes file data. I cannot use any 3rd party libraries. And what concerns me, is that Content-Length is zero - seems like file was not transferred at all :( Can anyone help me with this? Thanks in advance :)

    011011010110000101100011011010000110100101101110 0110010101110011

    C 1 Reply Last reply
    0
    • C csrss

      Hi there. I am looking for a simple, basic, preferable a C way of parsing multipart/form-data POST request from a client browser. So far I have a really simple web server, which reads web browser requests in a loop and prints them to to command prompt window:

      do
      {
      	len = recv(..., input\_buf, ...); 
      
      } 
          while (!strstr(input\_buf, "\\r\\n\\r\\n") && len > 0); 
      
      // Try to parse header and data
      char \* pch = nullptr, \*context = nullptr;
      pch = strtok\_s(input\_buf, "\\r\\n", &context);
      while (pch != nullptr)
      {
      	::puts(pch);
      	pch = strtok\_s (nullptr, "\\r\\n", &context);
      }
      

      When this web server starts, it just shows the following web form to the web browser:

      <form type=multipart/form-data method=post action=submit><input type=file multiple required /><input type=submit /></form>

      So far so good, ok, I connect to my server with firefox, select some test text file, which is a really small one - just a couple of bytes and hit an html form submit button. Server reads request and I have the following output in command prompt window:

      POST /submit HTTP/1.1
      Host: localhost:6666
      User-Agent: Mozilla/5.0 (Windows NT 6.2; WOW64; rv:24.0) Gecko/20100101 Firefox/
      24.0
      Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
      Accept-Language: en-US,en;q=0.5
      Accept-Encoding: gzip, deflate
      Referer: http://localhost:6666/
      Connection: keep-alive
      Content-Type: application/x-www-form-urlencoded
      Content-Length: 0

      But the question is, how do I get file data? I was reading tons of online docs, when it says something about boundary strings - I have no idea what are boundary strings. I am just trying to figure out how to grab raw bytes file data. I cannot use any 3rd party libraries. And what concerns me, is that Content-Length is zero - seems like file was not transferred at all :( Can anyone help me with this? Thanks in advance :)

      011011010110000101100011011010000110100101101110 0110010101110011

      C Offline
      C Offline
      csrss
      wrote on last edited by
      #2

      Figured it out - there is an error in my html form, it should be enctype, not type. Now I have a file content.

      011011010110000101100011011010000110100101101110 0110010101110011

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups