我正在探索在Linux平台上开发一个小型(简陋的)Web服务器,但我不知道从何开始。
我希望它能够做到以下几点:
- 监听特定端口
- 接收HTTP POST和GET请求
- 适当地响应请求
- 不需要会话管理
- 必须使用C或C ++编写
- 必须作为服务在启动时运行
我熟悉HTTP头,并且是一名经验丰富的PHP和.NET Web开发人员,但我不确定该如何开始这项任务。
您能否向我提供一些资源来帮助我度过学习曲线?
我正在探索在Linux平台上开发一个小型(简陋的)Web服务器,但我不知道从何开始。
我希望它能够做到以下几点:
我熟悉HTTP头,并且是一名经验丰富的PHP和.NET Web开发人员,但我不确定该如何开始这项任务。
您能否向我提供一些资源来帮助我度过学习曲线?
从上到下,你需要了解以下内容:
因此,您将需要学习编写基本的Unix应用程序,BSD套接字编程用于TCP/IP网络编程和HTTP协议。
常用文本包括:
Unix应用程序开发:
TCP/IP (sockets) 编程:
HTTP协议
对于一个简单/基础/超轻量级的HTTP服务器,程序流程应该像这样(伪代码):
----Main thread----
Load settings
while true do
Wait for connection
Connection received, create a new thread and transfer this connection to this thread.
end
----Connection thread----
Analyze request
if dynamic content do
Dump the HTTP request and send it to the interpreter
Wait for response from the interpreter
Read response header from the interpreter
Stream response
else if static content do
Load requested file
Stream file content
end
(Optional) Cache the response if size < X
Send the response
Close the socket
那么你应该学习Threading(线程),Interprocess-communication(进程间通信,如果你想与解释器交互),Socket编程和HTTP协议。
无法在此处解释所有细节
访问http://www.linuxhowtos.org/C_C++/socket.htm以使用C创建基本服务器。
IBM提供的另一个链接:http://www.ibm.com/developerworks/systems/library/es-nweb/index.html
http://en.wikipedia.org/wiki/Comparison_of_lightweight_web_servers
我再次感谢维基百科。
顺便说一句,你可能想要谷歌搜索“嵌入式Web服务器开源”。
(www).ibm.com/developerworks/web/library/wa-ltwebserv/