从0使用TCP手撸http服务器五

前面的例子我们只处理了一个url请求,在web中他们喜欢叫路由,那么我们也叫它路由吧。

添加路由功能:不同的路由应该反馈不同的页面

实现思路:理论上我们只需要解析出红框内的数据,就拿到路由了,然后判断属于什么路由,返回对应的页面

      

 查找路由代码:

//查找字串位置
static int find_str(char * str1,int len1,char *str2,int len2)
{
    int i=0;
    int j=0;
    for(i=0;i<len1-len2;i++)
    {
        for(j=0;j<len2;j++)
        {
            if((str1[i+j]!=str2[j]))
            {
                break;
            }
        }
        //printf("for %d %d\r\n",j,len2);
        if(j==len2)
        {
            return i;
        }
    }
    return 0;
}


char route_str[100];
//寻找路由
static char * find_route(char *str1)
{

    int route_index =find_str(str1,strlen(str1),"/",strlen("/"));

    //printf("route index:%d\r\n",route_index);

    //找到路由结束位置
    int route_index_end =find_str(str1+route_index,strlen(str1)," ",strlen(" "));

    //printf("route index end:%d\r\n",route_index_end);

    //取出路由
    for(int i=0;i<route_index_end;i++)
    {

        route_str[i]=str1[route_index+i];
    }
    route_str[route_index_end]='\0';

    return route_str;
}

流程处理:

			printf("浏览器发送:\n");
			printf("len %d\r\n",recv_len);
			printf("%s",recv_buf);
			
			//寻找路由
			char *route=find_route(recv_buf);

			printf("route:%s\r\n",route);
				
			if(strcmp("/favicon.ico",route)==0)
			{
				closesocket(acpt_soc);
				printf("\r\nsock close\r\n");
				break;
			}
					
			if(strcmp("/",route)==0)
			{

				//这里等于发送数据给浏览器
				send_html(acpt_soc,index_page);
			}
			if(strcmp("/info",route)==0)
			{

				//这里等于发送数据给浏览器
				send_html(acpt_soc,index_page);
			}

效果:

 

日志:

http server running...
[http-Web] client IP:[127.0.0.1], port:[53372]
浏览器发送:
len 719
GET / HTTP/1.1
Host: 127.0.0.1
Connection: keep-alive
Cache-Control: max-age=0
sec-ch-ua: "Microsoft Edge";v="111", "Not(A:Brand";v="8", "Chromium";v="111"
sec-ch-ua-mobile: ?0
sec-ch-ua-platform: "Windows"
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36 Edg/111.0.1661.51
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7
Sec-Fetch-Site: none
Sec-Fetch-Mode: navigate
Sec-Fetch-User: ?1
Sec-Fetch-Dest: document
Accept-Encoding: gzip, deflate, br
Accept-Language: zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6

route:/
客户端关闭:
[http-Web] client IP:[127.0.0.1], port:[53373]
浏览器发送:
len 614
GET /favicon.ico HTTP/1.1
Host: 127.0.0.1
Connection: keep-alive
sec-ch-ua: "Microsoft Edge";v="111", "Not(A:Brand";v="8", "Chromium";v="111"
sec-ch-ua-mobile: ?0
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36 Edg/111.0.1661.51
sec-ch-ua-platform: "Windows"
Accept: image/webp,image/apng,image/svg+xml,image/*,*/*;q=0.8
Sec-Fetch-Site: same-origin
Sec-Fetch-Mode: no-cors
Sec-Fetch-Dest: image
Referer: http://127.0.0.1/
Accept-Encoding: gzip, deflate, br
Accept-Language: zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6

route:/favicon.ico

sock close
[http-Web] client IP:[127.0.0.1], port:[53374]
浏览器发送:
len 614
GET /favicon.ico HTTP/1.1
Host: 127.0.0.1
Connection: keep-alive
sec-ch-ua: "Microsoft Edge";v="111", "Not(A:Brand";v="8", "Chromium";v="111"
sec-ch-ua-mobile: ?0
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36 Edg/111.0.1661.51
sec-ch-ua-platform: "Windows"
Accept: image/webp,image/apng,image/svg+xml,image/*,*/*;q=0.8
Sec-Fetch-Site: same-origin
Sec-Fetch-Mode: no-cors
Sec-Fetch-Dest: image
Referer: http://127.0.0.1/
Accept-Encoding: gzip, deflate, br
Accept-Language: zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6

route:/favicon.ico

sock close
[http-Web] client IP:[127.0.0.1], port:[53376]
浏览器发送:
len 614
GET /favicon.ico HTTP/1.1
Host: 127.0.0.1
Connection: keep-alive
sec-ch-ua: "Microsoft Edge";v="111", "Not(A:Brand";v="8", "Chromium";v="111"
sec-ch-ua-mobile: ?0
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36 Edg/111.0.1661.51
sec-ch-ua-platform: "Windows"
Accept: image/webp,image/apng,image/svg+xml,image/*,*/*;q=0.8
Sec-Fetch-Site: same-origin
Sec-Fetch-Mode: no-cors
Sec-Fetch-Dest: image
Referer: http://127.0.0.1/
Accept-Encoding: gzip, deflate, br
Accept-Language: zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6

route:/favicon.ico

sock close
[http-Web] client IP:[127.0.0.1], port:[53379]
浏览器发送:
len 719
GET / HTTP/1.1
Host: 127.0.0.1
Connection: keep-alive
Cache-Control: max-age=0
sec-ch-ua: "Microsoft Edge";v="111", "Not(A:Brand";v="8", "Chromium";v="111"
sec-ch-ua-mobile: ?0
sec-ch-ua-platform: "Windows"
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36 Edg/111.0.1661.51
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7
Sec-Fetch-Site: none
Sec-Fetch-Mode: navigate
Sec-Fetch-User: ?1
Sec-Fetch-Dest: document
Accept-Encoding: gzip, deflate, br
Accept-Language: zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6

route:/
客户端关闭:
[http-Web] client IP:[127.0.0.1], port:[53380]
浏览器发送:
len 614
GET /favicon.ico HTTP/1.1
Host: 127.0.0.1
Connection: keep-alive
sec-ch-ua: "Microsoft Edge";v="111", "Not(A:Brand";v="8", "Chromium";v="111"
sec-ch-ua-mobile: ?0
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36 Edg/111.0.1661.51
sec-ch-ua-platform: "Windows"
Accept: image/webp,image/apng,image/svg+xml,image/*,*/*;q=0.8
Sec-Fetch-Site: same-origin
Sec-Fetch-Mode: no-cors
Sec-Fetch-Dest: image
Referer: http://127.0.0.1/
Accept-Encoding: gzip, deflate, br
Accept-Language: zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6

route:/favicon.ico

sock close
[http-Web] client IP:[127.0.0.1], port:[53381]
浏览器发送:
len 614
GET /favicon.ico HTTP/1.1
Host: 127.0.0.1
Connection: keep-alive
sec-ch-ua: "Microsoft Edge";v="111", "Not(A:Brand";v="8", "Chromium";v="111"
sec-ch-ua-mobile: ?0
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36 Edg/111.0.1661.51
sec-ch-ua-platform: "Windows"
Accept: image/webp,image/apng,image/svg+xml,image/*,*/*;q=0.8
Sec-Fetch-Site: same-origin
Sec-Fetch-Mode: no-cors
Sec-Fetch-Dest: image
Referer: http://127.0.0.1/
Accept-Encoding: gzip, deflate, br
Accept-Language: zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6

route:/favicon.ico

sock close
[http-Web] client IP:[127.0.0.1], port:[53382]
浏览器发送:
len 614
GET /favicon.ico HTTP/1.1
Host: 127.0.0.1
Connection: keep-alive
sec-ch-ua: "Microsoft Edge";v="111", "Not(A:Brand";v="8", "Chromium";v="111"
sec-ch-ua-mobile: ?0
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36 Edg/111.0.1661.51
sec-ch-ua-platform: "Windows"
Accept: image/webp,image/apng,image/svg+xml,image/*,*/*;q=0.8
Sec-Fetch-Site: same-origin
Sec-Fetch-Mode: no-cors
Sec-Fetch-Dest: image
Referer: http://127.0.0.1/
Accept-Encoding: gzip, deflate, br
Accept-Language: zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6

route:/favicon.ico

sock close

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值