Unix项目编写Web服务器

Web服务器

功能:显示静态页面



基本知识 :HTTP协议

  1. URL: 统一资源定位符
    在互联网范围内唯一地标识一个特定的资源,如文件、应用,或者API等。
http://www.tmooc.cn/login.html                                  
http://www.tmooc.cn/files/login.html:8080  ...
           
http   www.tmooc.cn   files/login.html    8080        ...        
协议      主机域名          路径              端口号        参数      
           <— DNS       
应用层   服务器IP地址 web服务器解释 区分服务   输入    
客户机处理    ——————————                            <—— 服务器处理 

在这里插入图片描述

  1. HTTP请求
    A. 包头
方法 路径 协议及版本 意义
GET /files/login.html HTTP/1.0\r\n //方法、路径和协议
HOST: www.tmooc.cn\r\n //主机域名
Accept: text/html\r\n // 可接受内容类型
Conncetion: keep-alive\r\n // 期望连接模式
User-Agent: Mozilla/5.0\r\n // 兼容浏览器
Referer: www.tmooc.cn\r\n\r\n // 引用范围

B. 包体
对于GET方法,包体为空。
对于POST方法,包体包含需要上传的数据。

  1. HTTP响应
    A. 包头
  协议及版本   响应码及解释 
        |                |           |
  HTTP/1.1       200       OK \r\n                       //协议和响应信息
  Data: Tue, 14 Apr 2021 08:24:34 GMT\r\n    //处理时期和时间
  Content-Type: text/html\r\n                             //内容类型 
  Content-Length: 2028                                      // 内容长度   
  Connection: close\r\n                                       //实际连接模式
  Server:tarena \r\n\r\n                                     //服务器别名       

B. 包体
《html》
页面内容或处理结果
《/html》



需求分析

明确系统的功能要求

   —URL—|       |———— HTTP请求                                      
   /             V     V                        V                                            
用户——>浏览器 ———— > Web服务器 <—— > 服务器资源
  |__________________  HTTP响应_______|                        


概要设计

设计系统的整体架构

                        主模块(main函数)                         
                                    |                                          
                            服 务 器 模块 —— 信号处理模块
                                    |                                           
协议处理模块 —— 客户线程模块 ——资源管理模块 
                                    |                                            
                            网络通信模块                               
                                    |                                             
                            客户机(浏览器)                             


详细设计

每个模块的内部结构:

  • 函数的名称、参数表和返回值
  • 类的名称、成员、访问控制属性和继承关系
  • 引入必要的抽象提高代码的伸缩性和复用性
  • 借鉴成熟的设计模式


编写代码

编程实现每个模块的具体功能
设计顺序: 自顶向下,逐层细化。
实现顺序:自底向上,逐层抽象。



协议处理模块

http.h、http.c ——> http.o
写好的头文件: 研发单位:达内科技——研发人员:闵卫

参考代码:mime.h


// mime.h - MIME类型
//
// 项目名称:WebServer (多线程版)
// 研发单位:达内科技
// 研发人员:闵卫
// 创建时间:2017年5月17日
// 修改记录:
//
#ifndef _MIME_H
#define _MIME_H

static struct {
   
 char suffix[256];
 char type[256];
}	s_mime[] = {
   
 {
   "."       , "application/x-"                         },
 {
   ".*"      , "application/octet-stream"               },
 {
   ".001"    , "application/x-001"                      },
 {
   ".301"    , "application/x-301"                      },
 {
   ".323"    , "text/h323"                              },
 {
   ".906"    , "application/x-906"                      },
 {
   ".907"    , "drawing/907"                            },
 {
   ".acp"    , "audio/x-mei-aac"                        },
 {
   ".ai"     , "application/postscript"                 },
 {
   ".aif"    , "audio/aiff"                             },
 {
   ".aifc"   , "audio/aiff"                             },
 {
   ".aiff"   , "audio/aiff"                             },
 {
   ".a11"    , "application/x-a11"                      },
 {
   ".anv"    , "application/x-anv"                      },
 {
   ".apk"    , "application/vnd.android.package-archive"},
 {
   ".asa"    , "text/asa"                               },
 {
   ".asf"    , "video/x-ms-asf"                         },
 {
   ".asp"    , "text/asp"                               },
 {
   ".asx"    , "video/x-ms-asf"                         },
 {
   ".au"     , "audio/basic"                            },
 {
   ".avi"    , "video/avi"                              },
 {
   ".awf"    , "application/vnd.adobe.workflow"         },
 {
   ".biz"    , "text/xml"                               },
 {
   ".bmp"    , "application/x-bmp"                      },
 {
   ".bot"    , "application/x-bot"                      },
 {
   ".c4t"    , "application/x-c4t"                      },
 {
   ".c90"    , "application/x-c90"                      },
 {
   ".cal"    , "application/x-cals"                     },
 {
   ".cat"    , "application/vnd.ms-pki.seccat"          },
 {
   ".cdf"    , "application/x-netcdf"                   },
 {
   ".cdr"    , "application/x-cdr"                      },
 {
   ".cel"    , "application/x-cel"                      },
 {
   ".cer"    , "application/x-x509-ca-cert"             },
 {
   ".cg4"    , "application/x-g4"                       },
 {
   ".cgm"    , "application/x-cgm"                      },
 {
   ".cit"    , "application/x-cit"                      },
 {
   ".class"  , "java/*"                                 },
 {
   ".cml"    , "text/xml"                               },
 {
   ".cmp"    , "application/x-cmp"                      },
 {
   ".cmx"    , "application/x-cmx"                      },
 {
   ".cot"    , "application/x-cot"                      },
 {
   ".crl"    , "application/pkix-crl"                   },
 {
   ".crt"    , "application/x-x509-ca-cert"             },
 {
   ".csi"    , "application/x-csi"                      },
 {
   ".css"    , "text/css"                               },
 {
   ".cut"    , "application/x-cut"                      },
 {
   ".dbf"    , "application/x-dbf"                      },
 {
   ".dbm"    , "application/x-dbm"                      },
 {
   ".dbx"    , "application/x-dbx"                      },
 {
   ".dcd"    , "text/xml"                               },
 {
   ".dcx"    , "application/x-dcx"                      },
 {
   ".der"    , "application/x-x509-ca-cert"             },
 {
   ".dgn"    , "application/x-dgn"                      },
 {
   ".dib"    , "application/x-dib"                      },
 {
   ".dll"    , "application/x-msdownload"               },
 {
   ".doc"    , "application/msword"                     },
 {
   ".dot"    , "application/msword"                     },
 {
   ".drw"    , "application/x-drw"                      },
 {
   ".dtd"    , "text/xml"                               },
 {
   ".dwf"    , "application/x-dwf"                      },
 {
   ".dwg"    , "application/x-dwg"                      },
 {
   ".dxb"    , "application/x-dxb"                      },
 {
   ".dxf"    , "application/x-dxf"                      },
 {
   ".edn"    , "application/vnd.adobe.edn"              },
 {
   ".emf"    , "application/x-emf"                      },
 {
   ".eml"    , "message/rfc822"                         },
 {
   ".ent"    , "text/xml"                               },
 {
   ".epi"    , "application/x-epi"                      },
 {
   ".eps"    , "application/x-ps"                       },
 {
   ".eps"    , "application/postscript"                 },
 {
   ".etd"    , "application/x-ebx"                      },
 {
   ".exe"    , "application/x-msdownload"               },
 {
   ".fax"    , "image/fax"                              },
 {
   ".fdf"    , "application/vnd.fdf"                    },
 {
   ".fif"    , "application/fractals"                   },
 {
   ".fo"     , "text/xml"                               },
 {
   ".frm"    , "application/x-frm"                      },
 {
   ".g4"     , "application/x-g4"                       },
 {
   ".gbr"    , "application/x-gbr"                      },
 {
   ".gif"    , "image/gif"                              },
 {
   ".gl2"    , "application/x-gl2"                      },
 {
   ".gp4"    , "application/x-gp4"                      },
 {
   ".hgl"    , "application/x-hgl"                      },
 {
   ".hmr"    , "application/x-hmr"                      },
 {
   ".hpg"    , "application/x-hpgl"                     },
 {
   ".hpl"    , "application/x-hpl"                      },
 {
   ".hqx"    , "application/mac-binhex40"               },
 {
   ".hrf"    , "application/x-hrf"                      },
 {
   ".hta"    , "application/hta"                        },
 {
   ".htc"    , "text/x-component"                       },
 {
   ".htm"    , "text/html"                              },
 {
   ".html"   , "text/html"                              },
 {
   ".htt"    , "text/webviewhtml"                       },
 {
   ".htx"    , "text/html"                              },
 {
   ".icb"    , "application/x-icb"                      },
 {
   ".ico"    , "image/x-icon"                           },
 {
   ".iff"    , "application/x-iff"                      },
 {
   ".ig4"    , "application/x-g4"                       },
 {
   ".igs"    , "application/x-igs"                      },
 {
   ".iii"    , "application/x-iphone"                   },
 {
   ".img"    , "application/x-img"                      },
 {
   ".ins"    , "application/x-internet-signup"          },
 {
   ".ipa"    , "application/vnd.iphone"                 },
 {
   ".isp"    , "application/x-internet-signup"          },
 {
   ".IVF"    , "video/x-ivf"                            },
 {
   ".java"   , "java/*"                                 },
 {
   ".jfif"   , "image/jpeg"                             },
 {
   ".jpe"    , "image/jpeg"                             },
 {
   ".jpeg"   , "image/jpeg"                             },
 {
   ".jpg"    , "image/jpeg"                             },
 {
   ".js"     , "application/x-javascript"               },
 {
   ".jsp"    , "text/html"                              },
 {
   ".la1"    , "audio/x-liquid-file"                    },
 {
   ".lar"    , "application/x-laplayer-reg"             },
 {
   ".latex"  , "application/x-latex"                    },
 {
   ".lavs"   , "audio/x-liquid-secure"                  },
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值