简体   繁体   English

使用 winsock 接收 http 请求

[英]Receiving http requests with winsock

For educational purposes I am trying to make a web api in c++.出于教育目的,我正在尝试用 C++ 制作一个 web api。 the web api needs to be able to listen for http requests(GET, POST etc.), when it receives a http request it needs to be able to send data back to the client. web api 需要能够侦听 http 请求(GET、POST 等),当它接收到 http 请求时,它需要能够将数据发送回客户端。 Because it is for educational purposes I would like to do it without unnecessary libraries.因为它是出于教育目的,所以我想在没有不必要的图书馆的情况下做到这一点。 Now the first thing I need to do is make the api able to receive requests and respond on that, after some research on google I found out that winsock is probably the most basic way to setup sockets for windows but I could find very little on receiving http requests.现在我需要做的第一件事是让 api 能够接收请求并对此做出响应,经过对谷歌的一些研究后,我发现 winsock 可能是为 Windows 设置套接字的最基本方法,但我在接收方面几乎找不到http 请求。

My question is : Is it possible with winsock to receive a http request from the browser, and send data back to the browser?.我的问题是:winsock 是否可以从浏览器接收 http 请求,并将数据发送回浏览器?。

My question is: Is it possible with winsock to receive a http request from the browser, and send data back to the browser?我的问题是:使用 winsock 是否可以从浏览器接收 http 请求,并将数据发送回浏览器?

Yes.是的。 ^^ ^^

It is, Because HTTP is a protocol that (usually) uses TCP as the underlying transportation protocol.是的,因为 HTTP 是一种(通常)使用 TCP 作为底层传输协议的协议。

But trying to build a real HTTP layer on top of a simple win32 socket is a bit too much even for an experienced C++ developer.但是,即使对于有经验的 C++ 开发人员来说,尝试在简单的 win32 套接字之上构建真正的 HTTP 层也有点过分。

Many un-experienced C++ developers would probably dismiss this task as a "well, you just need to read some data, parse the headers, assemble your own HTTP response and send it back".许多没有经验的 C++ 开发人员可能会将这项任务视为“好吧,您只需要读取一些数据、解析标头、组装您自己的 HTTP 响应并将其发送回”。

but.. You will have to support但是..你必须支持

  • TLS, with all the nasty private keys/public keys implementation TLS,带有所有讨厌的私钥/公钥实现
  • Redirection重定向
  • Chunked Transfer分块传输
  • G-Zip transfer G-Zip 传输

and the list goes on and on..这样的例子不胜枚举..

So practically speaking, if you just want to to accept a socket, read some data and send some basic HTTP response than yes.所以实际上,如果你只是想接受一个套接字,读取一些数据并发送一些基本的 HTTP 响应,那么是的。 If you want a reliable, professional HTTP library - probably no.如果您想要一个可靠、专业的 HTTP 库 - 可能不需要。

You can check this page https://github.com/ReneNyffenegger/cpp-webserver to see simple winsock server implementation for HTTP.您可以查看此页面https://github.com/ReneNyffenegger/cpp-webserver以查看 HTTP 的简单 winsock 服务器实现。 Web server implementation is not so difficult. Web 服务器的实现并不是那么困难。 Of course you should have time for it.当然,你应该有时间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM