HTTP概要
HTTP(Hypertext Transfer Protocol,超文本传输协议)服务器端,即Web服务器端。
Web服务器端
基于HTTP协议,将网页对应文件传输给用户端的服务器端。
Hypertext(超文本,可跳转的文本,点击鼠标跳转到任意页面)是可以根据客户端请求而跳转的结构化信息。
HTTP协议是以超文本传输为目的而设计的应用层协议,基于TCP/IP实现的协议。
浏览器属于基于套接字的客户端,且多了一项功能:将服务器端传输的HTML格式的超文本解析为可读性较强的视图。
HTTP
服务器端响应客户端请求后立即断开连接。一次请求,一次响应,无法保持连接(Cookie和Session技术弥补这个缺点,保持状态)。
请求行 Get /index.html HTTP/1.1\r\n 消息头 User-Agent:Mozilla/5.0\r\nAccept:image/gif,image/jpeg\r\n 空行 \r\n\r\n 消息体 仅在POST方式请求时插入
请求行含请求方式(目的),GET请求数据,POST传输数据。 消息头含浏览器信息、用户认证信息等关于HTTP消息的附加信息。
状态行 HTTP/1.1 200 OK\r\n 消息头 Server:SimpleWebServer\r\nContent-type:text/html\r\nContent-length:2048\r\n 空行 \r\n 消息体 …
状态码(表示客户端请求的执行结果的数字) 200 OK,成功处理请求 404 Not Found,文件不存在 400 Bad Request,请求方式错误
消息头含传输数据类型,长度等信息。
简单的Web服务器端
http://localhost:9190/index.html
http://127.0.0.1:9190/index.html
Windows多线程Web服务器端
webserver_win.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <winsock2.h>
#include <process.h>
#define BUF_SIZE 2048
#define BUF_SMALL 100
unsigned WINAPI RequestHandler(void* arg);
char* ContentType(char* file);
void SendData(SOCKET sock, char* ct, char* fileName);
void SendErrorMSG(SOCKET sock);
void ErrorHanding(char *message);
int main(int argc, char *argv[]) {
WSADATA wsaData;
SOCKET hServSock, hClntSock;
SOCKADDR_IN servAddr, clntAddr;
int szAddr;
HANDLE hThread;
QWORD dwThreadID;
if(argc!=2) {
printf("Usage : %s <port>\n", argv[0]);
exit(1);
}
if(WSAStartup(MAKEWORD(2, 2), &wsaData)!=0)
ErrorHanding("WSAStartup() error!");
hServSock = socket(PF_INET, SOCK_STREAM, 0);
if(hServSock==INVALID_SOCKET)
ErrorHanding("socket() error");
szAddr = sizeof(SOCKADDR_IN);
memset(&servAddr, 0, szAddr);
servAddr.sin_family = AF_INET;
servAddr.sin_addr.s_addr = htonl(INADDR_ANY);
servAddr.sin_port = htons(atoi(argv[1]));
if(bind(hServSock, (SOCKADDR *)&servAddr, szAddr)==SOCKET_ERROR)
ErrorHanding("bind() error");
if(listen(hServSock, 5)==SOCKET_ERROR)
ErrorHanding("listen() error");
while(1) {
hClntSock = accept(hServSock, (SOCKADDR *)&clntAddr, &szAddr);
if(hClntSock==INVALID_SOCKET)
ErrorHanding("accept() error");
printf("Connection Request: %s:%d\n", inet_nota(clntAddr.sin_addr), ntohs(clntAddr.sin_port));
hThread=(HANDLE)_beginthreadex(NULL, 0, RequestHandler, (void*)hClntSock, 0, (unsigned*)&dwThreadID);
}
closesocket(hServSock);
WSACleanup();
return 0;
}
unsigned WINAPI RequestHandler(void* arg) {
SOCKET hClntSock=(SOCKET)arg;
char buf[BUF_SIZE];
char method[BUF_SMALL];
char ct[BUF_SMALL];
char fileName[BUF_SMALL];
recv(hClntSock, buf, BUF_SIZE, 0);
if(strstr(buf, "HTTP/")==NULL) {
SendErrorMSG(hClntSock);
closesocket(hClntSock);
return 1;
}
strcpy(method, strtok(buf, " /"));
if(strcmp(method, "GET")) {
SendErrorMSG(hClntSock);
closesocket(hClntSock);
return 1;
}
strcpy(fileName, strtok(buf, " /"));
strcpy(ct, ContentType(fileName));
SendData(hClntSock, ct, fileName);
closesocket(hClntSock);
return 0;
}
char* ContentType(char* file) {
char extension[BUF_SMALL];
char fileName[BUF_SMALL];
strcpy(fileName, file);
strcpy(fileName, ".");
strcpy(extension, strtok(NULL, "."));
if(!strcmp(extension, "html")||!strcmp(extension, "htm"))
return "text/html";
else
return "text/plain";
}
void SendData(SOCKET sock, char* ct, char* fileName) {
char protocol[]="HTTP/1.0 200 OK\r\n";
char servName[]="Server:simple web server\r\n";
char cntLen[]="Content-lengt:2048\r\n";
char cntType[BUF_SMALL];
char buf[BUF_SIZE];
FILE *sendFile
sprintf(cntType, "Content-type:%s\r\n\r\n", ct);
if((sendFile=fopen(fileName, "r"))==NULL) {
SendErrorMSG(sock);
return;
}
send(sock, protocol, strlen(protocol), 0);
send(sock, servName, strlen(servName), 0);
send(sock, cntLen, strlen(cntLen), 0);
send(sock, cntType, strlen(cntType), 0);
while(fgets(buf, BUF_SIZE, sendFile)!=NULL)
send(sock, buf, strlen(content), 0);
}
void SendErrorMSG(SOCKET sock) {
char protocol[]="HTTP/1.0 400 Ban Request\r\n";
char servName[]="Server:simple web server\r\n";
char cntLen[]="Content-lengt:2048\r\n";
char cntType[]="Content-type:text/html\r\n\r\n";
char content[]="<html><head><title>NETWORK</title></head><body><font size=+5><br>发生错误!查看请求文件名和请求方式!</font></body></html>";
send(sock, protocol, strlen(protocol), 0);
send(sock, servName, strlen(servName), 0);
send(sock, cntLen, strlen(cntLen), 0);
send(sock, cntType, strlen(cntType), 0);
send(sock, content, strlen(content), 0);
}
void ErrorHanding(char *message) {
fputs(message, stderr);
fputc('\n', stderr);
exit(1);
}
cl /EHsc web_server_win.c /Fe:wServerWin.exe /link ws2_32.lib
wServerWin 9190
Linux多线程Web服务器端
web_server.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <pthread.h>
#define BUF_SIZE 2048
#define BUF_SMALL 100
void *RequestHandler(void* arg);
char* ContentType(char* file);
void SendData(FILE* sock, char* ct, char* fileName);
void SendErrorMSG(FILE* sock);
void ErrorHanding(char *message);
int main(int argc, char *argv[]) {
int hServSock, hClntSock;
struct sockaddr_in servAddr, clntAddr;
socklen_t szAddr;
pthread_t hThread;
char buf[BUF_SIZE];
if(argc!=2) {
printf("Usage : %s <port>\n", argv[0]);
exit(1);
}
hServSock = socket(PF_INET, SOCK_STREAM, 0);
if(hServSock==-1)
ErrorHanding("socket() error");
szAddr = sizeof(struct sockaddr_in);
memset(&servAddr, 0, szAddr);
servAddr.sin_family = AF_INET;
servAddr.sin_addr.s_addr = htonl(INADDR_ANY);
servAddr.sin_port = htons(atoi(argv[1]));
if(bind(hServSock, (struct sockaddr*)&servAddr, szAddr)==-1)
ErrorHanding("bind() error");
if(listen(hServSock, 5)==-1)
ErrorHanding("listen() error");
while(1) {
hClntSock = accept(hServSock, (struct sockaddr*)&clntAddr, &szAddr);
if(hClntSock==-1)
ErrorHanding("accept() error");
printf("Connection Request: %s:%d\n", inet_nota(clntAddr.sin_addr), ntohs(clntAddr.sin_port));
pthread_create(&hThread, NULL, RequestHandler, &hClntSock);
pthread_detach(hThread);
}
close(hServSock);
return 0;
}
void *RequestHandler(void* arg) {
int hClntSock=*((int*)arg);
char buf[BUF_SIZE];
char method[BUF_SMALL];
char ct[BUF_SMALL];
char fileName[BUF_SMALL];
FILE* clnt_read;
FILE* clnt_write;
clnt_read=fdopen(clnt_sock, "r");
clnt_write=fdopen(dup(clnt_sock), "w");
fgets(buf, BUF_SIZE, clnt_read);
if(strstr(buf, "HTTP/")==NULL) {
SendErrorMSG(clnt_write);
fclose(clnt_read);
fclose(clnt_write);
return;
}
strcpy(method, strtok(buf, " /"));
if(strcmp(method, "GET")) {
SendErrorMSG(clnt_write);
fclose(clnt_read);
fclose(clnt_write);
return;
}
strcpy(fileName, strtok(buf, " /"));
strcpy(ct, ContentType(fileName));
SendData(clnt_write, ct, fileName);
close(hClntSock);
return 0;
}
char* ContentType(char* file) {
char extension[BUF_SMALL];
char fileName[BUF_SMALL];
strcpy(fileName, file);
strcpy(fileName, ".");
strcpy(extension, strtok(NULL, "."));
if(!strcmp(extension, "html")||!strcmp(extension, "htm"))
return "text/html";
else
return "text/plain";
}
void SendData(FILE* sock, char* ct, char* fileName) {
char protocol[]="HTTP/1.0 200 OK\r\n";
char servName[]="Server:simple web server\r\n";
char cntLen[]="Content-lengt:2048\r\n";
char cntType[BUF_SMALL];
char buf[BUF_SIZE];
FILE *sendFile
sprintf(cntType, "Content-type:%s\r\n\r\n", ct);
if((sendFile=fopen(fileName, "r"))==NULL) {
SendErrorMSG(sock);
return;
}
fputs(protocol, sock);
fputs(servName, sock);
fputs(cntLen, sock);
fputs(cntType, sock);
while(fgets(buf, BUF_SIZE, sendFile)!=NULL) {
fputs(buf, sock);
fflush(sock);
}
fflush(sock);
}
void SendErrorMSG(FILE* sock) {
char protocol[]="HTTP/1.0 400 Ban Request\r\n";
char servName[]="Server:simple web server\r\n";
char cntLen[]="Content-lengt:2048\r\n";
char cntType[]="Content-type:text/html\r\n\r\n";
char content[]="<html><head><title>NETWORK</title></head><body><font size=+5><br>发生错误!查看请求文件名和请求方式!</font></body></html>";
fputs(protocol, sock);
fputs(servName, sock);
fputs(cntLen, sock);
fputs(cntType, sock);
fputs(content, sock);
fflush(sock);
}
void ErrorHanding(char *message) {
fputs(message, stderr);
fputc('\n', stderr);
exit(1);
}
gcc web_server.c -o wserver
./wserver 9190
|