0

I am trying to build a web server using sockets. I have the parser for GET requests and now I want to be able to send an image to the server from a form.

After printing the request header this is what I got:

POST / HTTP/1.1
Host: 127.0.0.1:8080
User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:71.0) Gecko/20100101 Firefox/71.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
Content-Type: multipart/form-data; boundary=---------------------------15680533759245126501880539822
Content-Length: 14006
Origin: http://127.0.0.1:8080
Connection: keep-alive
Referer: http://127.0.0.1:8080/form.html
Upgrade-Insecure-Requests: 1

-----------------------------15680533759245126501880539822
Content-Disposition: form-data; name="textline"

4213124124123123153534123412
-----------------------------15680533759245126501880539822
Content-Disposition: form-data; name="datafile"; filename="5.jpg"
Content-Type: image/jpeg

����

This is my form:

<html>
    <head>
        <title>Test</title>
    </head>
    <body>
        <form action="/"
        enctype="multipart/form-data" method="post">
        <p>
            Type some text (if you like):<br>
            <input type="text" name="textline">
        </p>
        <p>
            Please specify a file, or a set of files:<br>
            <input type="file" name="datafile">
        </p>
            <div>
            <input type="submit" value="Send">
            </div>
        </form>
    </body>
</html>

My questions are: Why do I receive only 4 bytes from the image and what should I do to be able to save the data receive in a jpg file?

Mike Bear
  • 46
  • 8
  • Are you using Windows? – Shawn Dec 25 '19 at 23:58
  • 2
    Looks like you are printing as string and please check the file with any editor support HEX mode or dump the file as hex, to check if the fifth byte is zero (i.e. `'\0'`). – Yingyu YOU Dec 26 '19 at 02:17
  • You are right I totally forgot that images have `'\0'` in their file. Now I only have to figure how I should save it as an image. – Mike Bear Dec 26 '19 at 11:04

1 Answers1

0

After printing the request header this is what I got:

What you've printed is not the request header but the header and part of the body.

Why do I receive only 4 bytes from the image

You did not provide any code so it is impossible to tell for sure. But a typical problem is to assume that a single recv will be sufficient to retrieve all data. This is not the case. Instead you need to first read the header (which can be contained as part of the first recv but can be spread over multiple recv too), extract the length of the body (in this case Content-Length: 14006 but it might also be chunked transfer encoding) and then read the body. Then you need for a multipart/form-data body to treat the body as a multipart MIME message and extract the various parts.

None of this is trivial, both HTTP and MIME are more complex then they look if you just had a look at a few examples. If you really want to implement everything yourself then please read the (long) standards - they are there for a reason. I recommend instead that you just instead use existing libraries to deal with this.

Steffen Ullrich
  • 114,247
  • 10
  • 131
  • 172
  • Thank you for your answer. Indeed I did not know how complex would that be to be done in pure C. It is possible to handle this part (the save of the file) by a script (i.e. PHP)? – Mike Bear Dec 26 '19 at 10:32
  • @MikeBear: PHP has support for this built in - see for example [Parsing multipart form data](https://stackoverflow.com/questions/1075513/parsing-multipart-form-data). – Steffen Ullrich Dec 26 '19 at 11:17