M
M
miXtik7892022-03-15 16:18:51
ASP.NET
miXtik789, 2022-03-15 16:18:51

I'm making an http server from scratch, how to receive files?

I'm making an http server from scratch, how to receive files? here is the input:
POST /index.html HTTP/1.1
Host: 127.0.0.1
Connection: keep-alive
Content-Length: 93611
Cache-Control: max-age=0
sec-ch-ua: " Not A;Brand";v ="99", "Chromium";v="96", "Yandex";v="22"
sec-ch-ua-mobile: ?0
sec-ch-ua-platform: "Windows"
Upgrade-Insecure-Requests : 1
Origin: 127.0.0.1
Content-Type: multipart/form-data; boundary=----WebKitFormBoundaryb7zv68KmdK9Rtv4Q
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.174 YaBrowser/22.1.5.810 Yowser/2.5 Safari/537.36
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3; q=0.9
Sec-Fetch-Site: same-origin
Sec-Fetch-Mode: navigate
Sec-Fetch-User: ?1
Sec-Fetch-Dest: document
Referer: 127.0.0.1/index.html
Accept-Encoding: gzip, deflate , br
Accept-Language: ru,en;q=0.9

As I understood this is my file(----WebKitFormBoundaryb7zv68KmdK9Rtv4Q) how to convert to byte[]?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
C
calculator212, 2022-03-15
@calculator212

The post request looks something like this, where (data) is a file. The bottom line is that you read the boundary value
and use this value to look for the payload in which the file lies. Then parse the fields that lie inside, if necessary of course. And then you start reading data and writing the file until you meet the closing boundary.

POST / HTTP/1.1
Host: 127.0.0.1:80
Content-Length: 179
Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW

----WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="image"; filename="charlize.jpg"
Content-Type: image/jpeg

(data)
----WebKitFormBoundary7MA4YWxkTrZu0gW

A
Alexander Yudakov, 2022-03-15
@AlexanderYudakov

What you brought here is called HTTP headers.
The data follows the headers. The separator between headers and data is two newlines.
The length of the transmitted data is indicated in the "Content-Length" header in bytes: 93611
An example of how to work with "Content-Type: multipart/*; boundary=..." can be found here:
https://developer.mozilla.org /en-US/docs/Web/HTTP/...
Note that when boundary is used, "--" is added in front.

F
freeExec, 2022-03-15
@freeExec

string.GetBytes()but it's not your file. He must go further and thereContent-Length: 93611

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question