2016-10-20 87 views
-1

我編寫的解析函數此PSET但check50只檢查爲有效如下:解析得到錯誤check50 pset6

:) server.c exists 
:) server compiles 
:(HTTP/1.0 returns error code 505 
    \ expected output, but not "HTTP/1.1 400 Bad Request\r\nContent-Typ..." 
:(Method of 'abcGET' returns error code 405 
    \ expected output, but not "HTTP/1.1 400 Bad Request\r\nContent-Typ..." 
:(Method of 'GETabc' returns error code 405 
    \ expected output, but not "HTTP/1.1 400 Bad Request\r\nContent-Typ..." 
:(request-target without starting '/' returns error code 501 
    \ expected output, but not "HTTP/1.1 400 Bad Request\r\nContent-Typ..." 
:(request-target of abc/hello.php returns error code 501 
    \ expected output, but not "HTTP/1.1 400 Bad Request\r\nContent-Typ..." 
:(Requesting cat.exe returns error code 501 
    \ expected output, but not "HTTP/1.1 400 Bad Request\r\nContent-Typ..." 
:(Requesting non-existant file returns error code 404 
    \ expected output, but not "HTTP/1.1 400 Bad Request\r\nContent-Typ..." 
:) 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 

所以基本上只有空間的檢查工作。在比較字符串時,我想就我的代碼中的問題的位置提供一些指導。我想使用這些功能是有問題的,但經過幾個小時的研究後,我仍然沒有看到解決方案。我是一個初學者,我真的很感激一些幫助。由於

這是我的解析函數

bool parse(const char* line, char* abs_path, char* query) 
{ 
    // TODO 
    // Get the lenght of line 
    int l = strlen(line); 

    // Store the place in the string 
    int a = 0; 

    // Stores number of total spaces 
    int spaces = 0; 

    // Iterate in line 
    for (int i = 0; i < l; i++) 
    {// Look for spaces 
     if (isspace(line[i]) == 0) 
     { 
      spaces++; 
     } 
    } 
    // If there are more than the 2 required spaces 
    if (spaces > 2) 
    { 
     error(400); 
     printf("400 Bad Request\n"); 
     return false; 
    } 
    // If request - target doen't begin with "/" 

    // Look for the fist space 
    for (int i = 0; i < l; i++) 
    {// Look for space 
     if (isspace(line[i]) == 0) 
     { 
      a = i + 1; 
      break; 
     } 
    } 

    if (line[a] != '/') 
    { 
     error(501); 
     printf("501 not implemented\n"); 
     return false; 
    } 


    { 
     error(405); 
     printf("405 Method Not Allowed\n"); 
     return false; 
    } 

    // If request-target contains " 
    // Will store position of second space 
    int b = 0; 
    // Look for the second space 
    for (int i = a; i < l; i++) 
    { 
     if (isspace(line[i]) == 0) 
     { 
      b = i; 
      break; 
     } 
    } 
    // Find if there is a " in request-target 
    for (int i = a; i < b; i++) 
    { 
     if (line[i] == '"') 
     { 
      error(400); 
      printf("400 Bad Request\n"); 
      return false; 
     } 
    } 

    // If HTTP version isn't HTTP/1.1 
    char http[7] = ""; 
    for (int i = b + 1; i < (b + 1) + 7/*Chars 
    from the second space till the end of HTTP/1.1*/;i++) 
    { 
     http[i] = line[i]; 
    } 

    if (strcmp(http, "HTTP/1.1") != 0) 
    { 
     error(505); 
     printf("505 HTTP Version Not Supported\n"); 
     return false; 
    } 

    // Store absolute-path at the address in abs_path and the query string at query 

    // Separate the query 
    // String to store the query 
    char query2[8190 + 1] = ""; 

    // Store the position in line where the query string starts 
    int c = 0; 
    // Counter for query array 
    int q = 0; 
    for (int i = b - 1/*before the space after the query*/; i > a/*after the first space*/;i--) 
    { 
     if (line[i] == '=') 
     { 
      c = i - 1; 
      break; 
     } 

    } 
    // Pass the string to query2 
    for (int i = c; i < (b - 1); i++) 
    { 
     query2[q] = line[i]; 
     q++; 
    } 
    // If there is no query, make query2 empty 
    if (query2[0] != 'q') 
    { 
     strcpy(query2, ""); 
    } 

    // Allocate query2 and query at the heap 
    query = malloc(sizeof(query2)); 

    // Copy query2 to query 
    strcpy(query, query2); 

    // Separate absolute path 
    // Declare char array to store absolute path 
    char abs1[8190 + 1] = ""; 

    // Make space at the heap 

    abs_path = malloc(sizeof(abs1)); 

    for (int i = a /*where the/after the first space starts*/; i < (c - 1); 
    i++) 
    { 
     abs1[i] = line[i]; 
    } 

    // Copy abs to abs_path 
    strcpy(abs_path, abs1); 

    return true; 


} 

回答

0

解析總是返回「400錯誤的請求」的代碼,這表明該條件:

if (spaces > 2) 

總是evaluting爲true,是因爲行有兩個或兩個以上的空格,但這是因爲http請求的格式需要一個空格將方法與請求分開,另一個空格將請求與http版本分開。

您需要搜索兩個連續空間,而不是整行中的兩個空格。

而你應該使用推薦的功能,而不是大量的for循環。

+0

感謝您的幫助@Daniel P.,我會研究一下 – JLDG