コード例 #1
0
ファイル: http.c プロジェクト: Godning/uhttp
char* get_request_line(char* buffer, http_request* request){

    char method[10];
    char path[1024];
    char version[20];
    char *query;

    strcpy(method, strsep(&buffer, " "));
    strcpy(path, strsep(&buffer, " "));
    strcpy(version, strsep(&buffer, "\r\n"));
    query = index(path, '?');
    if(query != NULL){
        *query = '\0';
        query++;
        request->query = strdup(query);
    }

    logoutf("%s %s %s\r\n",method, path, version);

    request->path = strdup(path);

    if (strcmp(method, "GET") == 0) {
        request->method = METHOD_GET;
    } else if (strcmp(method, "POST") == 0) {
        request->method = METHOD_POST;
    }
    request->version = get_http_version(version);

    return buffer + 1;  //  skip the \n at last
}
コード例 #2
0
static int http_get_parse(char *req, int len, get_ret_t *ret)
{
	char *s = req, *e, *end;
	char *path;
	int path_len, minor_version;

	end = req + len;

	if (memcmp_fail_loc(s, "GET ", len, 4, &e)) goto malformed_request;

	path = remove_whitespace(e, len);
	e = find_whitespace(path, len - (path-s));
	path_len = e-path;
	e = remove_whitespace(e, len - (e-s));

	if (get_http_version(e, len - (e-s), &e, &minor_version)) goto malformed_request;
	if (minor_version != 0 && minor_version != 1) {
		/* This should be seen as an error: */
		e = s;
		goto malformed_request;
	}

	ret->head_flags = 0;
	if (http_find_header_end(e, len - (e-s), &e, ret->head_flags)) goto malformed_request;

	ret->end = e;
	ret->path = path;
	ret->path_len = path_len;
	return 0;

malformed_request:
	ret->path = NULL;
	ret->path_len = -1;
	ret->end = e;
	return -1;
}