资源简介
基于linux通过c实现web服务器,适合新手 学习了知识点,然后通过项目巩固一下知识点,也可以了解http协议。

代码片段和文件信息
/* J. David‘s webserver */
/* This is a simple webserver.
* Created November 1999 by J. David Blackstone.
* CSE 4344 (Network concepts) Prof. Zeigler
* University of Texas at Arlington
*/
/* This program compiles for Sparc Solaris 2.6.
* To compile for Linux:
* 1) Comment out the #include line.
* 2) Comment out the line that defines the variable newthread.
* 3) Comment out the two lines that run pthread_create().
* 4) Uncomment the line that runs accept_request().
* 5) Remove -lsocket from the Makefile.
*/
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define ISspace(x) isspace((int)(x))
#define SERVER_STRING “Server: jdbhttpd/0.1.0\r\n“
void accept_request(int);
void bad_request(int);
void cat(int FILE *);
void cannot_execute(int);
void error_die(const char *);
void execute_cgi(int const char * const char * const char *);
int get_line(int char * int);
void headers(int const char *);
void not_found(int);
void serve_file(int const char *);
int startup(u_short *);
void unimplemented(int);
/**********************************************************************/
/* A request has caused a call to accept() on the server port to
* return. Process the request appropriately.
* Parameters: the socket connected to the client */
/**********************************************************************/
void accept_request(int client)
{
char buf[1024];
int numchars;
char method[255];
char url[255];
char path[512];
size_t i j;
struct stat st;
int cgi = 0; /* becomes true if server decides this is a CGI
* program */
char *query_string = NULL;
numchars = get_line(client buf sizeof(buf));
i = 0; j = 0;
while (!ISspace(buf[j]) && (i < sizeof(method) - 1))
{
method[i] = buf[j];
i++; j++;
}
method[i] = ‘\0‘;
if (strcasecmp(method “GET“) && strcasecmp(method “POST“))
{
unimplemented(client);
return;
}
if (strcasecmp(method “POST“) == 0)
cgi = 1;
i = 0;
while (ISspace(buf[j]) && (j < sizeof(buf)))
j++;
while (!ISspace(buf[j]) && (i < sizeof(url) - 1) && (j < sizeof(buf)))
{
url[i] = buf[j];
i++; j++;
}
url[i] = ‘\0‘;
if (strcasecmp(method “GET“) == 0)
{
query_string = url;
while ((*query_string != ‘?‘) && (*query_string != ‘\0‘))
query_string++;
if (*query_string == ‘?‘)
{
cgi = 1;
*query_string = ‘\0‘;
query_string++;
}
}
sprintf(path “htdocs%s“ url);
if (path[strlen(path) - 1] == ‘/‘)
strcat(path “index.html“);
if (stat(path &st) == -1) {
while ((numchars > 0) && strcmp(“\n“ buf)) /* read & discard headers */
numchars = get_line(client buf sizeof(buf));
not_found(client);
}
else
{
if ((st.st_mode & S_IFMT) == S_IFDIR)
strcat(path “/index.html“);
if ((st.st_mode & S
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
目录 0 2017-08-14 23:20 http服务器\
目录 0 2017-08-14 23:20 http服务器\htdocs\
文件 479 2017-08-14 23:20 http服务器\htdocs\check.cgi
文件 320 2017-08-14 23:20 http服务器\htdocs\color.cgi
文件 218 2017-08-14 23:20 http服务器\htdocs\index.html
文件 308 2017-08-14 23:20 http服务器\htdocs\README
文件 14537 2017-08-14 23:20 http服务器\httpd.c
文件 95 2017-08-14 23:20 http服务器\Makefile
文件 1904 2017-08-14 23:20 http服务器\README
文件 673 2017-08-14 23:20 http服务器\simpleclient.c
- 上一篇:nrf2401程序(收发都有注释清晰)
- 下一篇:IR2136应用笔记
相关资源
- STM32F103RC+ADC+DMA多通道采样LCD显示
- Modbus协议官方文档中、英文全
- pcbtemp电流计算软件
- I2C读写AT24C02 基于STM32F103 cube116540
- Scratch源码
- Microsoft Forms 2.0107770
- 实验三 消息中间件应用开发:Active
- WCE注入工具
- ModelGoon-4.4.1-site.zip
- AsyncTask文件控制暂停和继续,在状态
- Visio大全模具(含Cisco、IBM等常用拓扑
- 信号奇异点Lipschitz指数计算
- 虚拟光驱deamon 适用于64位服务器的
- 基于STM32RCT6的步进电机驱动程序
- 酒店管理系统基于Qt Creator5)
- 用友NC开发API字典
- Navicat Premium 15汉化包.zip55438
- 登录注册界面.zip48872
- 条码字体barcode128
- Rational Rose Common破解文件
- res10_300x300_ssd_iter_140000.caffemodel与dep
- scratch 第1课 翻跟斗的小猫(入门)
- stm32f407上的两个can发送和接收例程
- Scrach 欢乐狙击手.sb2
- 04741计算机网络原理知识点整理.docx(
- Wolfram Mathematica 矩阵初等变换函数(
- pscad近海风电模型 Fortran语言
- 程序员专用字体YaHei.Consolas.1.11b42517
- scratch3.0 源程序(说相声)
- AutoCAD永久去教育版破解补丁
评论
共有 条评论