1. 程式人生 > >c語言實現簡單web服務器

c語言實現簡單web服務器

tps gate choices found lte expect inf tro condition

1http簡單介紹

http超文本傳輸協議:host主機地址:port端口/url

host會被DNS服務器 解析成IP地址,所以有時候可以直接用域名,

http默認訪問80端口,https默認訪問443端口


大致流程就是:瀏覽器輸入地址後,首先和web服務器建立tcp連接,

然後瀏覽器發送http請求報文, web服務器響應處理這個報文,

然後給他回復一個響應,然後服務器主動斷開連接。





2http請求報文格式

技術分享圖片


首先第一個就是請求的方法,方法有一下這些:
GET,POST,HEAD,PUT,DELETE,OPTIONS,TRACE,CONNECT;


1GET

在瀏覽器輸入的網址,瀏覽器就會發送GET的http報文請求。

如果不寫url 默認就是 "/" 服務器 可根據這個響應對應的頁面.

頭部信息就包含一些重要的請求信息,如主機地址.

瀏覽器版本 , 手機的頁面就是根據這個去做的.

GET 攜帶參數是在url裏面的, POST是攜帶在包體裏面的.

包體成為body,請求頭部叫做head。

GET傳遞參數,格式 /url?username=xxx&passwd=bbb 通過問號解析參數部分

url的傳遞參數是有限制的,每個瀏覽器限制都不一樣。url不允許有回車換行



2POST

POST也是一個請求操作,他的數據參數攜帶在http請求的body裏面。

所有的參數都不允許有回車換行的存在, 很多時候如果必須要攜帶

回車換行的話,必須先把數據轉換成base64編碼,因為它沒有回車換成.他是解決網絡傳輸的常用方法。





3http響應報文格式

技術分享圖片


1狀態碼:請求是否成功,狀態碼描述:成功或失敗的原因

有時候訪問一網頁 會出現404,這個404就是這個狀態碼.




4http數據傳輸模式

1傳輸中兩個重要的參數: 寫在頭裏面

transfer-encoding:identity,chunked表示當前這個body是什麽協議發過來的

content-length:length:數據包的長度



2identity 直接發送模式,length在後面表示數據長度


3chunked 模式,後面跟的是每一個chunk包 [包頭,包數據]

包頭:第一個字節表示一個ASSIC數據,第二個字節也是ASSIC數據

兩個字節加起來,組成一個16進制數據。

後面兩個字節,固定的0d0a(回車換行符)兩個字節這4個字節就是一個chunk的包頭,

後面的數據包 根據前面的兩個字節來決定。數據包的結束標誌是

30 0d 0a 30ascll碼代表的是0

也就是說chunk包的結束:是遇到一個為等於0的chunk結束。

然後把這個包整合一下,形成完整的數據。





5http狀態碼和表

/*

{

[100] = "Continue",

[101] = "Switching Protocols",

[200] = "OK",

[201] = "Created",

[202] = "Accepted",

[203] = "Non-Authoritative Information",

[204] = "No Content",

[205] = "Reset Content",

[206] = "Partial Content",

[300] = "Multiple Choices",

[301] = "Moved Permanently",

[302] = "Found",

[303] = "See Other",

[304] = "Not Modified",

[305] = "Use Proxy",

[307] = "Temporary Redirect",

[400] = "Bad Request",

[401] = "Unauthorized",

[402] = "Payment Required",

[403] = "Forbidden",

[404] = "Not Found",

[405] = "Method Not Allowed",

[406] = "Not Acceptable",

[407] = "Proxy Authentication Required",

[408] = "Request Time-out",

[409] = "Conflict",

[410] = "Gone",

[411] = "Length Required",

[412] = "Precondition Failed",

[413] = "Request Entity Too Large",

[414] = "Request-URI Too Large",

[415] = "Unsupported Media Type",

[416] = "Requested range not satisfiable",

[417] = "Expectation Failed",

[500] = "Internal Server Error",

[501] = "Not Implemented",

[502] = "Bad Gateway",

[503] = "Service Unavailable",

[504] = "Gateway Time-out",

[505] = "HTTP Version not supported",

}

*/





6使用http_parmer解析 URL讀取 然後返回給客戶端

//解析我們的http報文
http_parser p;
http_parser_init(&p,HTTP_REQUEST);
http_parser_settings s;
http_parser_settings_init(&s);
//解析到了url 回調
s.on_url = ws_on_url;
//重置解析信息
init_ws_params();
// 解析器執行。返回解析的字節數
http_parser_execute(&p,&s,http_req,len);
//設置回調函數
switch (p.method)  //報文響應的方式
{
case HTTP_GET:
{
int len_URL = filter_url(WS_HTTP.url);
//訪問的網頁  
if (strncmp("/", WS_HTTP.url, len_URL) == 0){ //訪問test這個html
strncpy(WS_HTTP.url, "www_root/index.html", strlen("www_root/index.html"));
}
else if (strncmp("/test", WS_HTTP.url, len_URL) == 0){ //訪問默認的url 根目錄
strncpy(WS_HTTP.url, "www_root/test.html", strlen("www_root/test.html"));
}
char* file_data = open_files(WS_HTTP.url);
//發送報文  也就是響應客戶端
//釋放內存
free(file_data);
}
break;
case HTTP_POST:
break;
}
//end
printf("\n");


讀取文件
static char* 
open_files(char* filename)
{
	//讀取這個文件
	FILE* f = fopen(filename,"rb");
	//文件大小
	int file_size = 0;
	fseek(f,0,SEEK_END);
	file_size = ftell(f);
	//指針又到文件頭
	fseek(f, 0, 0);

	char* file_data = malloc(file_size + 1);
	fread(file_data, file_size,1, f);
	file_data[file_size] = 0;


	fclose(f);
	return file_data;
	
}




7響應請求報文

//使用identity來發送響應報文 
static void
write_ok_identity(int sock, char* body)
{
int len = strlen(body);
//使用直接模式 transfer-encoding:identity
char* send_line = malloc(len + 8096);
memset(send_line, 0, sizeof(send_line));
//回應http的頭
sprintf(send_line, "HTTP/1.1  %d %s\r\n", 200, "OK");
//設置頭部的一些信息  比如傳送模式 body的長度
char* walk = send_line;
//跳過這個頭部
walk = walk + strlen(walk);
sprintf(walk,"transfer-encoding:%s\r\n","identity");
//body的長度
walk = walk + strlen(walk);  //頭結束
sprintf(walk, "content-length: %d\r\n\r\n0", len);
//寫入數據部分
walk = walk + strlen(walk);
sprintf(walk, "%s", body);
//發送報文 響應  給客戶端
send(sock, send_line, strlen(send_line), 0);
free(send_line);
walk = NULL;
}





c語言實現簡單web服務器