【HTTP】HTTP解析

本文详细介绍了一个HTTP报文解析过程,包括请求行和请求头部的解析方法。通过具体的代码实现,展示了如何从原始报文中提取出HTTP方法、URL、版本及头部信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

首先我们来看一下HTTP报文的结构
在这里插入图片描述

解析请求行

int process_status_line(char *start, char *end, struct http_request *httpRequest) {
	int size = end - start;
	//method
	char *space = strstr(start," ");
	assert(space != NULL);
	int method_size = space - start;
	httpRequest->method = malloc(method_size + 1);
	strncpy(httpRequest->method, start, method_size);
	httpRequest->method[method_size] = '\0';
	
	//url
	start = space + 1;
	space = strstr(start," ");
	assert(space != NULL);
	int url_size = space - start;
	httpRequest->url = malloc(url_size + 1);
	strncpy(httpRequest->url, start, url_size);
	httpRequest->url[url_size] = '\0';
	
	//version
	start = space + 1;
	space = strstr(start," ");
	assert(space != NULL);
	int version_size = space - start;
	httpRequest->version = malloc(version_size + 1);
	strncpy(httpRequest->version, start, version_size);
	httpRequest->version[version_size] = '\0';
	
	return size;
	
}

解析请求头部

int parse_http_request(struct Buffer *buf, struct http_request *httpRequest) {
	int ok = 1;
	int cnt = 0;
	while(httpRequest->current_state != REQUEST_DONE && cnt < 100) {
		cnt++;
		if (httpRequest->current_state == REQUEST_STATUS) {
			printf("httpRequest->current_state == REQUEST_STATUS\n");
			printf("buf->read_index= %d, buf->write->index=%d\n", buf->read_index, buf->write_index);
			char *crlf = strstr(buf->data + buf->read_index, "\r\n");
			if (crlf != NULL) {
				int status_line_size = process_status_line(buf->data + buf->read_index, crlf, httpRequest);
				if (status_line_size) {
					buf->read_index = status_line_size + 2; //crlf + 2
					httpRequest->current_state = REQUEST_HEADERS;   //等待解析headers
					printf("REQUEST_STATUS done!\n");
				}
			} else printf("crlf error!\n");
			
		} else if (httpRequest->current_state == REQUEST_HEADERS) {
			printf("httpRequest->current_state == REQUEST_HEADERS\n");
			char *crlf = strstr(buf->data + buf->read_index,"\r\n");
			if (crlf) {
				char *start = buf->data + buf->read_index;
				char *colon = strstr(start, ": ");
				if (colon != NULL) {
					char *key = malloc(colon - start + 1);
					strncpy(key, start, colon - start);
					key[crlf - start] = '\0';
					
					char *val = malloc(crlf - colon - 1);
					strncpy(val, colon + 2, crlf - colon - 2);
					val[crlf - colon - 2] = '\0';
					httpRequest->request_headers[httpRequest->request_header_num].key = key;
					httpRequest->request_headers[httpRequest->request_header_num].val = val;
					httpRequest->request_header_num++;
					
					buf->read_index = crlf + 2 - buf->data;
				} else {
					buf->read_index = crlf + 2 - buf->data;
					httpRequest->current_state = REQUEST_DONE;
				}
			}
		}
	}
	return ok;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值