解析函数C错误 [英] Parse function C error

查看:88
本文介绍了解析函数C错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的PSET6解析函数的代码。我不知道为什么我会失败501。 (我没有被要求为404做一个。)



~ / workspace / pset6 / $ check50 2015.fall.pset6.server1 server.c

:) server.c存在

:)服务器编译

:) HTTP / 1.0返回错误代码505

: ''abcGET'方法返回错误代码405

:)'GETabc'方法返回错误代码405

:) request-target而不启动'/'返回错误代码501

:)请求目标abc / hello.php返回错误代码501

:(请求cat.exe返回错误代码501

\期望输出,但不是HTTP / 1.1 505 HTTP版本不支持...

:(请求不存在的文件返回错误代码404

\ expected输出,但不是HTTP / 1.1 505 HTTP版本不支持...

:)请求请求目标返回错误代码400

:)两个空格后GET返回错误代码

:)请求目标中的空格返回错误代码

:) HTTP / 1.1之前的两个空格返回错误代码

https://sandbox.cs50.net/checks/09d731e26ba744e6b2c086cb1b67cfe5



我尝试了什么:



Here is my code for the parse function for PSET6. I am not sure why I get the 501 fail. (I'm not told to do the one for 404).

~/workspace/pset6/ $ check50 2015.fall.pset6.server1 server.c
:) server.c exists
:) server compiles
:) HTTP/1.0 returns error code 505
:) Method of 'abcGET' returns error code 405
:) Method of 'GETabc' returns error code 405
:) request-target without starting '/' returns error code 501
:) request-target of abc/hello.php returns error code 501
:( Requesting cat.exe returns error code 501
\ expected output, but not "HTTP/1.1 505 HTTP Version Not Supported..."
:( Requesting non-existant file returns error code 404
\ expected output, but not "HTTP/1.1 505 HTTP Version Not Supported..."
:) Requesting request-target with " returns error code 400
:) Two spaces after GET returns error code
:) A space within the request target returns error code
:) Two spaces before HTTP/1.1 returns error code
https://sandbox.cs50.net/checks/09d731e26ba744e6b2c086cb1b67cfe5

What I have tried:

bool parse(const char* line, char* abs_path, char* query)
{   
    char* buffer = malloc(sizeof(line));
    strcpy( buffer, line);
    
    char* firstsp = strtok( buffer, " ");
    char* scndsp = strtok( NULL, " "); 
    char* thirdsp = strtok( NULL, " "); 
    char* ver = strtok( thirdsp, "\\");
    
    char method[ strlen(firstsp) + 1 ];
    char request[ strlen(scndsp) + 1 ];
    char http[ strlen(ver) + 1 ];
    
    strcpy( method, firstsp);
    strcpy( request, scndsp);
    strcpy( http, ver);
    
     if ( strcmp(method, "GET") != 0 )
     {
         error(405);
         return false;
     }

     if ( request[0] != '/')
     {
         error(501);
         return false;
     }
     
    if ( strchr( request, '\"') != NULL )
    {
        error(400);
        return false;
    }
            
    if (strcmp( http, "HTTP/1.1") != 0)
    {
        error(505);
        return false;
    }
    
    abs_path = request;
    
    int b;
    for ( b = 0; b < strlen(scndsp); b++ )
    {
        if ( scndsp[b] == '?' )    
        {
            break;
        }
    }
    
    if ( strchr( scndsp, '=') != NULL)
    {
        while (scndsp[b] != ' ' )
        {
            query[b] = scndsp[b];
            b++;
        }
    }
    else
    {
        query[0] = '\0';
    }
    return true;
}

推荐答案

check50 2015.fall.pset6.server1 server.c

:) server.c存在

:)服务器compi les

:) HTTP / 1.0返回错误代码505

:)'abcGET'方法返回错误代码405

:)方法' GETabc'返回错误代码405

:) request-target而不启动'/'返回错误代码501

:)请求目标abc / hello.php返回错误代码501

:(请求cat.exe返回错误代码501

\预期输出,但不是HTTP / 1.1 505 HTTP版本不支持...

:(请求不存在的文件返回错误代码404

\预期输出,但不是HTTP / 1.1 505 HTTP版本不支持...

:)请求目标请求返回错误代码400

:) GET后两个空格返回错误代码

:)请求目标中的空格返回错误代码

:) HTTP / 1.1之前的两个空格返回错误代码

https://sandbox.cs50.net/checks/09d731e26ba744e6b2c086cb1b67cfe5



我尝试过:



check50 2015.fall.pset6.server1 server.c
:) server.c exists
:) server compiles
:) HTTP/1.0 returns error code 505
:) Method of 'abcGET' returns error code 405
:) Method of 'GETabc' returns error code 405
:) request-target without starting '/' returns error code 501
:) request-target of abc/hello.php returns error code 501
:( Requesting cat.exe returns error code 501
\ expected output, but not "HTTP/1.1 505 HTTP Version Not Supported..."
:( Requesting non-existant file returns error code 404
\ expected output, but not "HTTP/1.1 505 HTTP Version Not Supported..."
:) Requesting request-target with " returns error code 400
:) Two spaces after GET returns error code
:) A space within the request target returns error code
:) Two spaces before HTTP/1.1 returns error code
https://sandbox.cs50.net/checks/09d731e26ba744e6b2c086cb1b67cfe5

What I have tried:

bool parse(const char* line, char* abs_path, char* query)
{   
    char* buffer = malloc(sizeof(line));
    strcpy( buffer, line);
    
    char* firstsp = strtok( buffer, " ");
    char* scndsp = strtok( NULL, " "); 
    char* thirdsp = strtok( NULL, " "); 
    char* ver = strtok( thirdsp, "\\");
    
    char method[ strlen(firstsp) + 1 ];
    char request[ strlen(scndsp) + 1 ];
    char http[ strlen(ver) + 1 ];
    
    strcpy( method, firstsp);
    strcpy( request, scndsp);
    strcpy( http, ver);
    
     if ( strcmp(method, "GET") != 0 )
     {
         error(405);
         return false;
     }

     if ( request[0] != '/')
     {
         error(501);
         return false;
     }
     
    if ( strchr( request, '\"') != NULL )
    {
        error(400);
        return false;
    }
            
    if (strcmp( http, "HTTP/1.1") != 0)
    {
        error(505);
        return false;
    }
    
    abs_path = request;
    
    int b;
    for ( b = 0; b < strlen(scndsp); b++ )
    {
        if ( scndsp[b] == '?' )    
        {
            break;
        }
    }
    
    if ( strchr( scndsp, '=') != NULL)
    {
        while (scndsp[b] != ' ' )
        {
            query[b] = scndsp[b];
            b++;
        }
    }
    else
    {
        query[0] = '\0';
    }
    return true;
}


您的代码存在两个大问题。



There are two a big problems with your code.

char* buffer = malloc(sizeof(line));
strcpy( buffer, line);

line 的类型为 char * sizeof(line)因此将是指针的大小(4或8字节)。你可能想要的是

line is of type char* and sizeof(line) will be therefore the size of a pointer (4 or 8 bytes). What you probably want is

char* buffer = malloc(strlen(line) + 1);





strtok()调用在未找到上一个标记的后续调用中返回 NULL (解析停止在终止 NULL 字符)。



所以你的 scndsp thirdsp 指针可能是 NULL ,其中包含格式错误的请求字符串。但是你使用这些指针作为参数 strtok()(对于 ver ), strlen () strcpy()。所有这些函数都没有检查不是 NULL 的参数,而是使用地址零执行操作。



如果发生这种情况,您会将随机字符写入请求 http 字符串(地址为零的字节,直到 NULL byte)。



所以你必须检查 NULL 指针优先并返回相应的错误代码。



The strtok() calls return NULL on subsequent calls when the previous token was not found (parsing stopped at the terminating NULL character).

So your scndsp and thirdsp pointer might be NULL with malformed request strings. But you are using these pointers as arguments to strtok() (for ver), strlen(), and strcpy(). All these functions did not check the arguments for being not NULL but perform the operation using the address zero.

If that happens, you are coyping random characters to the request and http strings (the bytes contained at address zero until a NULL byte).

So you have to check for NULL pointers first and return corresponding error codes.


这篇关于解析函数C错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆