任务详情

基于华为鲲鹏云服务器CentOS中(或Ubuntu),使用Linux Socket实现:

  1. Web服务器的客户端服务器,提交程序运行截图
  2. 实现GET即可,请求,响应要符合HTTP协议规范
  3. 服务器部署到华为云服务器,浏览器用本机的
  4. 把服务器部署到试验箱。(加分项)

截图





代码

//copy.c
/* copy.c:
*
* Copyright (C) 1998 Kenneth Albanowski <kjahds@kjahds.com>,
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*/ #include <stdio.h> static char copybuf[16384]; extern int TIMEOUT; int copy(FILE *read_f, FILE *write_f)
{
int n;
int wrote;
alarm(TIMEOUT);
while (n = fread(copybuf,1,sizeof(copybuf),read_f)) {
alarm(TIMEOUT);
wrote = fwrite(copybuf,n,1,write_f);
alarm(TIMEOUT);
if (wrote < 1)
return -1;
}
alarm(0);
return 0;
}
//httpd.c
/* httpd.c: A very simple http server
* Copyfight (C) 2003 Zou jian guo <ah_zou@163.com>
* Copyright (C) 2000 Lineo, Inc. (www.lineo.com)
* Copyright (c) 1997-1999 D. Jeff Dionne <jeff@lineo.ca>
* Copyright (c) 1998 Kenneth Albanowski <kjahds@kjahds.com>
* Copyright (c) 1999 Nick Brok <nick@nbrok.iaehv.nl>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
*/ #include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <errno.h>
#include <sys/stat.h>
#include <dirent.h>
#include <signal.h>
#include <unistd.h>
#include <ctype.h>
#include "pthread.h" #define DEBUG int KEY_QUIT=0;
int TIMEOUT=30; #ifndef O_BINARY
#define O_BINARY 0
#endif char referrer[128];
int content_length; #define SERVER_PORT 80 int PrintHeader(FILE *f, int content_type)
{
alarm(TIMEOUT);
fprintf(f,"HTTP/1.0 200 OK\n");
switch (content_type)
{
case 't':
fprintf(f,"Content-type: text/plain\n");
break;
case 'g':
fprintf(f,"Content-type: image/gif\n");
break;
case 'j':
fprintf(f,"Content-type: image/jpeg\n");
break;
case 'h':
fprintf(f,"Content-type: text/html\n");
break;
}
fprintf(f,"Server: uClinux-httpd 0.2.2\n");
fprintf(f,"Expires: 0\n");
fprintf(f,"\n");
alarm(0);
return(0);
} int DoJpeg(FILE *f, char *name)
{
char *buf;
FILE * infile;
int count; if (!(infile = fopen(name, "r"))) {
alarm(TIMEOUT);
fprintf(stderr, "Unable to open JPEG file %s, %d\n", name, errno);
fflush(f);
alarm(0);
return -1;
} PrintHeader(f,'j'); copy(infile,f); /* prints the page */ alarm(TIMEOUT);
fclose(infile);
alarm(0); return 0;
} int DoGif(FILE *f, char *name)
{
char *buf;
FILE * infile;
int count; if (!(infile = fopen(name, "r"))) {
alarm(TIMEOUT);
fprintf(stderr, "Unable to open GIF file %s, %d\n", name, errno);
fflush(f);
alarm(0);
return -1;
} PrintHeader(f,'g'); copy(infile,f); /* prints the page */ alarm(TIMEOUT);
fclose(infile);
alarm(0); return 0;
} int DoDir(FILE *f, char *name)
{
char *buf;
DIR * dir;
struct dirent * dirent; if ((dir = opendir(name))== 0) {
fprintf(stderr, "Unable to open directory %s, %d\n", name, errno);
fflush(f);
return -1;
} PrintHeader(f,'h'); alarm(TIMEOUT);
fprintf(f, "<H1>Index of %s</H1>\n\n",name);
alarm(0); if (name[strlen(name)-1] != '/') {
strcat(name, "/");
} while(dirent = readdir(dir)) {
alarm(TIMEOUT); fprintf(f, "<p><a href=\"/%s%s\">%s</a></p>\n", name, dirent->d_name, dirent->d_name);
alarm(0);
} closedir(dir);
return 0;
} int DoHTML(FILE *f, char *name)
{
char *buf;
FILE *infile;
int count;
char * dir = 0; if (!(infile = fopen(name,"r"))) {
alarm(TIMEOUT);
fprintf(stderr, "Unable to open HTML file %s, %d\n", name, errno);
fflush(f);
alarm(0);
return -1;
} PrintHeader(f,'h');
copy(infile,f); /* prints the page */ alarm(TIMEOUT);
fclose(infile);
alarm(0); return 0;
} int DoText(FILE *f, char *name)
{
char *buf;
FILE *infile;
int count; if (!(infile = fopen(name,"r"))) {
alarm(TIMEOUT);
fprintf(stderr, "Unable to open text file %s, %d\n", name, errno);
fflush(f);
alarm(0);
return -1;
} PrintHeader(f,'t');
copy(infile,f); /* prints the page */ alarm(TIMEOUT);
fclose(infile);
alarm(0); return 0;
} int ParseReq(FILE *f, char *r)
{
char *bp;
struct stat stbuf;
char * arg;
char * c;
int e;
int raw; #ifdef DEBUG
printf("req is '%s'\n", r);
#endif while(*(++r) != ' '); /*skip non-white space*/
while(isspace(*r))
r++; while (*r == '/')
r++;
bp = r; while(*r && (*(r) != ' ') && (*(r) != '?'))
r++; #ifdef DEBUG
printf("bp='%s' %x, r='%s' \n", bp, *bp,r);
#endif if (*r == '?')
{
char * e;
*r = 0;
arg = r+1;
if (e = strchr(arg,' '))
{
*e = '\0';
}
} else
{
arg = 0;
*r = 0;
} c = bp; /*zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz*/
if (c[0] == 0x20){
c[0]='.';
c[1]='\0';
}
/*zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz*/
if(c[0] == '\0') strcat(c,"."); if (c && !stat(c, &stbuf))
{
if (S_ISDIR(stbuf.st_mode))
{
char * end = c + strlen(c);
strcat(c, "/index.html");
if (!stat(c, &stbuf))
{
DoHTML(f, c);
}
else
{
*end = '\0';
DoDir(f,c);
}
}
else if (!strcmp(r - 4, ".gif"))
DoGif(f,c);
else if (!strcmp(r - 4, ".jpg") || !strcmp(r - 5, ".jpeg"))
DoJpeg(f,c);
else if (!strcmp(r - 4, ".htm") || !strcmp(r - 5, ".html"))
DoHTML(f,c);
else
DoText(f,c);
}
else{
PrintHeader(f,'h');
alarm(TIMEOUT);
fprintf(f, "<html><head><title>404 File Not Found</title></head>\n");
fprintf(f, "<body>The requested URL was not found on this server</body></html>\n");
alarm(0);
}
return 0;
} void sigalrm(int signo)
{
/* got an alarm, exit & recycle */
exit(0);
} int HandleConnect(int fd)
{
FILE *f; char buf[160];
char buf1[160]; f = fdopen(fd,"a+");
if (!f) {
fprintf(stderr, "httpd: Unable to open httpd input fd, error %d\n", errno);
alarm(TIMEOUT);
close(fd);
alarm(0);
return 0;
}
setbuf(f, 0); alarm(TIMEOUT); if (!fgets(buf, 150, f)) {
fprintf(stderr, "httpd: Error reading connection, error %d\n", errno);
fclose(f);
alarm(0);
return 0;
}
#ifdef DEBUG
printf("buf = '%s'\n", buf);
#endif alarm(0); referrer[0] = '\0';
content_length = -1; alarm(TIMEOUT);
//read other line to parse Rrferrer and content_length infomation
while (fgets(buf1, 150, f) && (strlen(buf1) > 2)) {
alarm(TIMEOUT);
#ifdef DEBUG
printf("Got buf1 '%s'\n", buf1);
#endif
if (!strncasecmp(buf1, "Referer:", 8)) {
char * c = buf1+8;
while (isspace(*c))
c++;
strcpy(referrer, c);
}
else if (!strncasecmp(buf1, "Referrer:", 9)) {
char * c = buf1+9;
while (isspace(*c))
c++;
strcpy(referrer, c);
}
else if (!strncasecmp(buf1, "Content-length:", 15)) {
content_length = atoi(buf1+15);
}
}
alarm(0); if (ferror(f)) {
fprintf(stderr, "http: Error continuing reading connection, error %d\n", errno);
fclose(f);
return 0;
} ParseReq(f, buf); alarm(TIMEOUT);
fflush(f);
fclose(f);
alarm(0);
return 1;
} void* key(void* data)
{
int c;
for(;;){
c=getchar();
if(c == 'q' || c == 'Q'){
KEY_QUIT=1;
exit(10);
break;
}
} } int main(int argc, char *argv[])
{
int fd, s;
int len;
volatile int true = 1;
struct sockaddr_in ec;
struct sockaddr_in server_sockaddr; pthread_t th_key;
void * retval; signal(SIGCHLD, SIG_IGN);
signal(SIGPIPE, SIG_IGN);
signal(SIGALRM, sigalrm); chroot(".");
printf("starting httpd...\n");
printf("press q to quit.\n");
// chdir("/"); if (argc > 1 && !strcmp(argv[1], "-i")) {
/* I'm running from inetd, handle the request on stdin */
fclose(stderr);
HandleConnect(0);
exit(0);
} if((s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) == -1) {
perror("Unable to obtain network");
exit(1);
} if((setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (void *)&true,
sizeof(true))) == -1) {
perror("setsockopt failed");
exit(1);
} server_sockaddr.sin_family = AF_INET;
server_sockaddr.sin_port = htons(SERVER_PORT);
server_sockaddr.sin_addr.s_addr = htonl(INADDR_ANY); if(bind(s, (struct sockaddr *)&server_sockaddr,
sizeof(server_sockaddr)) == -1) {
perror("Unable to bind socket");
exit(1);
} if(listen(s, 8*3) == -1) { /* Arbitrary, 8 files/page, 3 clients */
perror("Unable to listen");
exit(4);
} pthread_create(&th_key, NULL, key, 0);
/* Wait until producer and consumer finish. */
printf("wait for connection.\n");
while (1) { len = sizeof(ec);
if((fd = accept(s, (void *)&ec, &len)) == -1) {
exit(5);
close(s);
}
HandleConnect(fd); }
pthread_join(th_key, &retval);
}

实验四 Web服务器2的更多相关文章

  1. 实验四 Web服务器1-socket编程

    一.任务详情基于华为鲲鹏云服务器CentOS中(或Ubuntu),使用Linux Socket实现: 1. time服务器的客户端服务器,提交程序运行截图 2. echo服务器的客户端服务器,提交程序 ...

  2. 学号20145332 《信息安全系统设计基础》实验五 简单嵌入式WEB服务器实验

    实验目的 掌握在 ARM 开发板实现一个简单 WEB 服务器的过程. 学习在 ARM 开发板上的 SOCKET 网络编程. 学习 Linux 下的 signal()函数的使用. 实验内容 学习使用 s ...

  3. 20145216 20145330 《信息安全系统设计基础》 实验五 简单嵌入式WEB 服务器实验

    20145216 20145330 <信息安全系统设计基础> 实验五 简单嵌入式WEB 服务器实验 实验报告封面 实验步骤 1.阅读理解源码 进入/arm2410cl/exp/basic/ ...

  4. 20145208《信息安全系统设计基础》实验五 简单嵌入式WEB 服务器实验

    20145208<信息安全系统设计基础>实验五 简单嵌入式WEB 服务器实验 20145208<信息安全系统设计基础>实验五 简单嵌入式WEB 服务器实验

  5. Linux下四款Web服务器压力测试工具(http_load、webbench、ab、siege)介绍

    一.http_load程序非常小,解压后也不到100Khttp_load以并行复用的方式运行,用以测试web服务器的吞吐量与负载.但是它不同于大多数压力测试工具,它可以以一个单一的进程运行,一般不会把 ...

  6. 20145210 20145226 《信息安全系统设计基础》实验五 简单嵌入式WEB服务器实验

    20145210 20145226 <信息安全系统设计基础>实验五 简单嵌入式WEB服务器实验 结对伙伴:20145226 夏艺华 实验报告封面 实验目的与要求 · 掌握在ARM开发板实现 ...

  7. 20145221 《信息安全系统设计基础》实验五 简单嵌入式WEB服务器实验

    20145221 <信息安全系统设计基础>实验五 简单嵌入式WEB服务器实验 实验报告 队友博客:20145326蔡馨熠 实验博客:<信息安全系统设计基础>实验五 简单嵌入式W ...

  8. 实验5 简单嵌入式WEB服务器实验 实验报告 20135303 20135326

    北京电子科技学院(BESTI) 实     验    报     告 课程:信息安全系统设计基础                班级:  1353 姓名:20135303 魏昊卿 学号:2013532 ...

  9. 20145303 20145339 《信息安全系统设计基础》 实验五 简单嵌入式WEB服务器实验

    20145303 20145339 <信息安全系统设计基础> 实验五 简单嵌入式WEB服务器实验 实验目的与要求 1.掌握在ARM开发板实现一个简单WEB服务器的过程 2.学习在ARM开发 ...

  10. 四 主要的几种 Web 服务器

    一 Microsoft IIS 1. 仅支持 Windows 操作系统,用于 .Net 平台网站的部署和运行. 2. IIS 是一种 Web 服务组件,包括括 Web 服务器.FTP 服务器.NNTP ...

随机推荐

  1. Git分支变基-知识点整理记录

    Git中分支的整合分为合并和变基两种. 变基是把一系列的提交按照原有次序依次应用到另一个分支上.而合并是把最终的结果合在一起. 一.变基原理 首先找到基底分支和当前分支的最近共同祖先,然后比对当前分支 ...

  2. 插入排序(CSP-J 2021 T2)

    题目:(由于题干过长直接上链接:P7910 [CSP-J 2021] 插入排序 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)) 不是打广告 又有一个新思路: 我们可以再开一个b数组 ...

  3. P16_发布-小程序的推广与运行数据的查看

    协同工作和发布 - 发布上线 基于小程序码进行推广 相对于普通二维码来说,小程序码的优势如下: 在样式上更具辨识度和视觉冲击力 能够更加清晰地树立小程序的品牌形象 可以帮助开发者更好地推广小程序 获取 ...

  4. STM32F1库函数初始化系列:DMA—ADC采集

    1 void ADC_Configure(void) 2 { 3 ADC_InitTypeDef ADC_InitStructure; 4 GPIO_InitTypeDef GPIO_InitStru ...

  5. Zstack使用经验系列2-安装的存储配置

    从上图读者应该能看出当初分配主存储和镜像存储时空间分配的是多么不合理,镜像空间不需要那么多.不过这时系统已经运行了近1年,很多云主机以及系统服务都搭好了,如果再重新分配空间是多么的麻烦! 所以开始为p ...

  6. 基于Vue3+TS的Monorepo前端项目架构设计与实现

    写在前面 你好,我是前端程序员鼓励师岩家兴!去年在另一个项目https://juejin.cn/post/7121736546000044046中,我向读者朋友们介绍了结合npm包管理工具yarn作v ...

  7. 图说论文《An Empirical Evaluation of In-Memory Multi-Version Concurrency Control》

    本文从< An Empirical Evaluation of In-Memory Multi-Version Concurrency Control>摘取部分图片,来介绍 MVCC. 该 ...

  8. Cobalt Strike 之:域内渗透

    郑重声明: 本笔记编写目的只用于安全知识提升,并与更多人共享安全知识,切勿使用笔记中的技术进行违法活动,利用笔记中的技术造成的后果与作者本人无关.倡导维护网络安全人人有责,共同维护网络文明和谐. Co ...

  9. Linux:touch 修改文件的时间

    修改本文件的时间 参数 描述 例子 -a 只修改访问时间(Access Time) touch -a hello.txt -m 只更新修改时间(Modify Time) touch -m hello. ...

  10. java数据结构与算法(day2)--简单排序

    模式:设计api实现api 简单排序 举例(商品排序) 1.1Comparable接口介绍(排序算法更有通用性:对象排序) 创建对象,并且生成豆子.创建Comparable接口 1 package c ...