解析Content-Type: multipart/form-data格式数据

经常要处理php端发过来的Content-Type: multipart/form-data数据,写两个函数处理下这个数据好。

2014-1-24

发现有个Bug(错误)!!!!


Content-Type: multipart/form-data; boundary=----------------------------aa14712544ce
 
------------------------------aa14712544ce
Content-Disposition: form-data; name="subject"
 
【186期】邂逅青岛,还未离开就想再见
------------------------------aa14712544ce
Content-Disposition: form-data; name="html"
 
Html/78/186soFksp7820130730_f7dJVlDX.html
------------------------------aa14712544ce
Content-Disposition: form-data; name="email"
 
[email protected]
------------------------------aa14712544ce
Content-Disposition: form-data; name="magid"
 
78
------------------------------aa14712544ce
Content-Disposition: form-data; name="number"
 
186
------------------------------aa14712544ce
Content-Disposition: form-data; name="nick"
 
MyTrip
------------------------------aa14712544ce--


#include 
#include 
#include 
#include 
#include 
#include 


char *get_boundary(char *mfd)
{
	char *p, *end;
	char *boundary = NULL;
	int boundary_size;
	
	if ((p = strstr(mfd, "boundary=")) == NULL) {
		return NULL;
	}

	p += 9;
	if ((end = strstr(p, "\r\n")) == NULL) {
		return NULL;
	}
	
	boundary_size = end - p + 1;
	boundary = malloc(boundary_size);

	strncpy(boundary, p, boundary_size);
	boundary[boundary_size - 1] = 0;
	
	return boundary;
}


char* mutipart_form_data(char *mfd, char *boundary, char **name, char **content)
{
	char *p, *end;
	int size;
	int boundary_size;

	*content = *name = NULL;
	if ( (p = strstr(mfd, "name=")) == NULL) {
		return -1;
	}
	p += 6;
	if ((end = strchr(p, '\"')) == NULL) {
		return -1;
	}

	// 获取name
	size = end - p + 1;
	*name = malloc(size);
	strncpy(*name, p, size);
	(*name)[size - 1] = 0;
	
	// 获取内容
	p = end + 1;
	if ((end = strstr(p, boundary)) == NULL) {
		free(*name);
		*name = NULL;
		return -1;
	}

	boundary_size = strlen(boundary);
	mfd = end + boundary_size;

	while(*p == '\r' || *p == '\n') {
		p++;
	}
	
	end -= 3;
	while(*end == '\r' || *end == '\n') {
		end--;
	}

	if (p > end) {
		free(*name);
		*name = NULL;
		return -1;
	}

	size = end - p + 2;
	*content = malloc(size);
	strncpy(*content, p, size);
	(*content)[size - 1] = 0;
	
	if (*(mfd + 1) == '-') {
		// 说明到了最后了
		return NULL;
	}

	return mfd;
}


int main(int argc, char *argv[])
{
	FILE *fp;
	char *f_buf;
	char *boundary;
	size_t f_size;
	fp = fopen(argv[1], "r");

	struct stat f_stat;
	stat(argv[1], &f_stat);
	f_size = f_stat.st_size;
	if (f_size <= 0) {
		printf("fail!!!!!!!\n");
		return 0;
	}
	
	f_buf = malloc(f_stat.st_size + 1);

	fread(f_buf, f_size, 1, fp);
	char *mfd = f_buf;

	
	boundary = get_boundary(f_buf);
	printf("boundary=%s\n", boundary);
	
	while (1) {
		char *name, *content;
		mfd = mutipart_form_data(mfd, boundary, &name, &content);
		if (mfd == -1) {
			break;
		}
                // show 
		printf("name=\"%s\" content=\"%s\"\n", name, content);
		free(name);
		free(content);
		if (mfd == NULL) {
			break;
		}
	}
	
	return 0;
}



你可能感兴趣的:(解析Content-Type: multipart/form-data格式数据)