📄 一个多线程web服务器实例(c,linux,详细的web服务器原理) - 马牛不是人 - csdnblog.htm
字号:
style="COLOR: #000000">"</SPAN><SPAN
style="COLOR: #000000">string comparing
:::::::::::::start::::::::::::::: </SPAN><SPAN
style="COLOR: #000000">"</SPAN><SPAN
style="COLOR: #000000">); <BR><BR></SPAN><SPAN
style="COLOR: #0000ff">#endif</SPAN><SPAN
style="COLOR: #000000"><BR><BR> </SPAN><SPAN
style="COLOR: #0000ff">if</SPAN><SPAN
style="COLOR: #000000">(strcmp(cmd,</SPAN><SPAN
style="COLOR: #000000">"</SPAN><SPAN
style="COLOR: #000000">GET</SPAN><SPAN
style="COLOR: #000000">"</SPAN><SPAN
style="COLOR: #000000">) </SPAN><SPAN
style="COLOR: #000000">==</SPAN><SPAN
style="COLOR: #000000"> </SPAN><SPAN
style="COLOR: #000000">0</SPAN><SPAN
style="COLOR: #000000">){<BR><BR> </SPAN><SPAN
style="COLOR: #008000">//</SPAN><SPAN
style="COLOR: #008000">the command is get</SPAN><SPAN
style="COLOR: #008000"><BR></SPAN><SPAN
style="COLOR: #000000"><BR>#ifdef DEBUG<BR><BR> printf(</SPAN><SPAN
style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">cmd(%s)==GET
</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN
style="COLOR: #000000">,cmd);<BR><BR></SPAN><SPAN
style="COLOR: #0000ff">#endif</SPAN><SPAN
style="COLOR: #000000"><BR><BR> </SPAN><SPAN
style="COLOR: #008000">//</SPAN><SPAN
style="COLOR: #008000">is this a file or dir or notexist?</SPAN><SPAN
style="COLOR: #008000"><BR></SPAN><SPAN
style="COLOR: #000000"><BR> </SPAN><SPAN
style="COLOR: #0000ff">if</SPAN><SPAN
style="COLOR: #000000">(fileordirExist(filepath)){<BR><BR> </SPAN><SPAN
style="COLOR: #008000">//</SPAN><SPAN
style="COLOR: #008000">is a file or dir or none<BR><BR> </SPAN><SPAN
style="COLOR: #008000">//</SPAN><SPAN
style="COLOR: #008000">is this a dir </SPAN><SPAN
style="COLOR: #008000"><BR></SPAN><SPAN
style="COLOR: #000000"><BR> </SPAN><SPAN
style="COLOR: #0000ff">if</SPAN><SPAN
style="COLOR: #000000">(IsDIR(filepath)){<BR><BR> </SPAN><SPAN
style="COLOR: #008000">//</SPAN><SPAN
style="COLOR: #008000">is a dir</SPAN><SPAN
style="COLOR: #008000"><BR></SPAN><SPAN
style="COLOR: #000000"><BR>#ifdef DEBUG<BR><BR> printf(</SPAN><SPAN
style="COLOR: #000000">"</SPAN><SPAN
style="COLOR: #000000">%s is a DIR </SPAN><SPAN
style="COLOR: #000000">"</SPAN><SPAN
style="COLOR: #000000">,filepath);<BR><BR></SPAN><SPAN
style="COLOR: #0000ff">#endif</SPAN><SPAN
style="COLOR: #000000"><BR><BR> </SPAN><SPAN
style="COLOR: #0000ff">if</SPAN><SPAN
style="COLOR: #000000">( fileordirExist( strcat(filepath,</SPAN><SPAN
style="COLOR: #000000">"</SPAN><SPAN
style="COLOR: #000000">index.htm</SPAN><SPAN
style="COLOR: #000000">"</SPAN><SPAN
style="COLOR: #000000">) )){<BR><BR> sendobj(connectfd,</SPAN><SPAN
style="COLOR: #000000">"</SPAN><SPAN
style="COLOR: #000000">index.htm</SPAN><SPAN
style="COLOR: #000000">"</SPAN><SPAN
style="COLOR: #000000">);<BR><BR> }</SPAN><SPAN
style="COLOR: #0000ff">else</SPAN><SPAN
style="COLOR: #000000"> </SPAN><SPAN
style="COLOR: #0000ff">if</SPAN><SPAN
style="COLOR: #000000">(fileordirExist(strcat(filepath,</SPAN><SPAN
style="COLOR: #000000">"</SPAN><SPAN
style="COLOR: #000000">index.html</SPAN><SPAN
style="COLOR: #000000">"</SPAN><SPAN
style="COLOR: #000000">))){<BR><BR> sendobj(connectfd,</SPAN><SPAN
style="COLOR: #000000">"</SPAN><SPAN
style="COLOR: #000000">index.htm</SPAN><SPAN
style="COLOR: #000000">"</SPAN><SPAN
style="COLOR: #000000">);<BR><BR> }</SPAN><SPAN
style="COLOR: #0000ff">else</SPAN><SPAN
style="COLOR: #000000">{<BR><BR> msg404(connectfd);<BR><BR> }<BR><BR> }</SPAN><SPAN
style="COLOR: #0000ff">else</SPAN><SPAN
style="COLOR: #000000">{<BR><BR> </SPAN><SPAN
style="COLOR: #008000">//</SPAN><SPAN
style="COLOR: #008000">is a file</SPAN><SPAN
style="COLOR: #008000"><BR></SPAN><SPAN
style="COLOR: #000000"><BR>#ifdef DEBUG<BR><BR> printf(</SPAN><SPAN
style="COLOR: #000000">"</SPAN><SPAN
style="COLOR: #000000">%s is a file</SPAN><SPAN
style="COLOR: #000000">"</SPAN><SPAN
style="COLOR: #000000">,filepath);<BR><BR></SPAN><SPAN
style="COLOR: #0000ff">#endif</SPAN><SPAN
style="COLOR: #000000"><BR><BR> sendobj(connectfd,filepath);<BR><BR> }<BR><BR> }</SPAN><SPAN
style="COLOR: #0000ff">else</SPAN><SPAN
style="COLOR: #000000">{<BR><BR>#ifdef DEBUG<BR><BR> printf(</SPAN><SPAN
style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">404
</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN
style="COLOR: #000000">);<BR><BR></SPAN><SPAN
style="COLOR: #0000ff">#endif</SPAN><SPAN
style="COLOR: #000000"><BR><BR> msg404(connectfd);<BR><BR> }<BR><BR> }</SPAN><SPAN
style="COLOR: #0000ff">else</SPAN><SPAN
style="COLOR: #000000">{<BR><BR>#ifdef DEBUG<BR><BR> printf(</SPAN><SPAN
style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">cmd(%s)!=GET
</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN
style="COLOR: #000000">,cmd);<BR><BR></SPAN><SPAN
style="COLOR: #0000ff">#endif</SPAN><SPAN
style="COLOR: #000000"><BR><BR> }<BR><BR>#ifdef DEBUG<BR><BR> printf(</SPAN><SPAN
style="COLOR: #000000">"</SPAN><SPAN
style="COLOR: #000000">:::::::::::::end::::::::::::::: </SPAN><SPAN
style="COLOR: #000000">"</SPAN><SPAN
style="COLOR: #000000">); <BR><BR></SPAN><SPAN
style="COLOR: #0000ff">#endif</SPAN><SPAN
style="COLOR: #000000"><BR><BR> close(connectfd);<BR><BR>}<BR><BR></SPAN><SPAN
style="COLOR: #008000">//</SPAN><SPAN
style="COLOR: #008000">send the 404 error message to the client</SPAN><SPAN
style="COLOR: #008000"><BR></SPAN><SPAN
style="COLOR: #000000"><BR></SPAN><SPAN
style="COLOR: #0000ff">void</SPAN><SPAN
style="COLOR: #000000"> msg404(</SPAN><SPAN
style="COLOR: #0000ff">int</SPAN><SPAN
style="COLOR: #000000"> connectfd)<BR><BR>{<BR><BR> </SPAN><SPAN
style="COLOR: #0000ff">char</SPAN><SPAN
style="COLOR: #000000">*</SPAN><SPAN
style="COLOR: #000000"> msg;<BR><BR> msg </SPAN><SPAN
style="COLOR: #000000">=</SPAN><SPAN
style="COLOR: #000000"> </SPAN><SPAN
style="COLOR: #000000">"</SPAN><SPAN
style="COLOR: #000000">HTTP/1.0 404 Not Found
Content-Type: text/plain
404 not found by Manio</SPAN><SPAN
style="COLOR: #000000">"</SPAN><SPAN
style="COLOR: #000000">;<BR><BR> send(connectfd,msg,strlen(msg),</SPAN><SPAN
style="COLOR: #000000">0</SPAN><SPAN
style="COLOR: #000000">);<BR><BR>}<BR><BR></SPAN><SPAN
style="COLOR: #008000">//</SPAN><SPAN
style="COLOR: #008000">is the filepath a file or directory</SPAN><SPAN
style="COLOR: #008000"><BR></SPAN><SPAN
style="COLOR: #000000"><BR></SPAN><SPAN
style="COLOR: #0000ff">int</SPAN><SPAN
style="COLOR: #000000"> fileordirExist(</SPAN><SPAN
style="COLOR: #0000ff">char</SPAN><SPAN
style="COLOR: #000000">*</SPAN><SPAN
style="COLOR: #000000"> fpath)<BR><BR>{<BR><BR> </SPAN><SPAN
style="COLOR: #0000ff">struct</SPAN><SPAN
style="COLOR: #000000"> stat filestat;<BR><BR> </SPAN><SPAN
style="COLOR: #0000ff">return</SPAN><SPAN
style="COLOR: #000000"> ( stat(fpath,</SPAN><SPAN
style="COLOR: #000000">&</SPAN><SPAN
style="COLOR: #000000">filestat) </SPAN><SPAN
style="COLOR: #000000">!=</SPAN><SPAN
style="COLOR: #000000"> </SPAN><SPAN
style="COLOR: #000000">-</SPAN><SPAN style="COLOR: #000000">1</SPAN><SPAN
style="COLOR: #000000">);<BR><BR>}<BR><BR><BR><BR></SPAN><SPAN
style="COLOR: #008000">//</SPAN><SPAN
style="COLOR: #008000"> is the filepath a directory</SPAN><SPAN
style="COLOR: #008000"><BR></SPAN><SPAN
style="COLOR: #000000"><BR></SPAN><SPAN
style="COLOR: #0000ff">int</SPAN><SPAN
style="COLOR: #000000"> IsDIR(</SPAN><SPAN
style="COLOR: #0000ff">char</SPAN><SPAN
style="COLOR: #000000">*</SPAN><SPAN
style="COLOR: #000000"> fpath)<BR><BR>{<BR><BR>#ifdef DEBUG<BR><BR> printf(</SPAN><SPAN
style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">IN IsDIR
</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN
style="COLOR: #000000">);<BR><BR></SPAN><SPAN
style="COLOR: #0000ff">#endif</SPAN><SPAN
style="COLOR: #000000"><BR><BR> </SPAN><SPAN
style="COLOR: #0000ff">struct</SPAN><SPAN
style="COLOR: #000000"> stat filestat;<BR><BR> </SPAN><SPAN
style="COLOR: #0000ff">return</SPAN><SPAN
style="COLOR: #000000"> ( stat(fpath,</SPAN><SPAN
style="COLOR: #000000">&</SPAN><SPAN
style="COLOR: #000000">filestat) </SPAN><SPAN
style="COLOR: #000000">!=</SPAN><SPAN
style="COLOR: #000000"> </SPAN><SPAN
style="COLOR: #000000">-</SPAN><SPAN style="COLOR: #000000">1</SPAN><SPAN
style="COLOR: #000000"> </SPAN><SPAN
style="COLOR: #000000">&&</SPAN><SPAN
style="COLOR: #000000"> S_ISDIR(filestat.st_mode));<BR><BR>}<BR><BR><BR><BR></SPAN><SPAN
style="COLOR: #008000">//</SPAN><SPAN
style="COLOR: #008000">send the data of the file which the client want</SPAN><SPAN
style="COLOR: #008000"><BR></SPAN><SPAN
style="COLOR: #000000"><BR></SPAN><SPAN
style="COLOR: #0000ff">int</SPAN><SPAN
style="COLOR: #000000"> sendobj(</SPAN><SPAN
style="COLOR: #0000ff">int</SPAN><SPAN
style="COLOR: #000000"> connectfd,</SPAN><SPAN
style="COLOR: #0000ff">char</SPAN><SPAN
style="COLOR: #000000">*</SPAN><SPAN
style="COLOR: #000000"> serverfilepath)<BR><BR>{<BR><BR> FILE</SPAN><SPAN
style="COLOR: #000000">*</SPAN><SPAN
style="COLOR: #000000"> sfp,</SPAN><SPAN
style="COLOR: #000000">*</SPAN><SPAN
style="C
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -