2016-10-15 73 views
0

我正在處理一個問題,我無法獲得abs_path和查詢數組充滿了我傳遞給他們的函數解析裏面的數據。在這個函數邏輯裏面似乎是正確的,我調試了它,並且兩個數組都填充了正確的數據。我知道我沒有在參數中傳遞一個指向數組(char **)的指針,因爲函數的參數不能被改變。任何其他建議解決這個問題?如何更改char *的內存地址?

#define LimitRequestLine 8190 
char abs_path[LimitRequestLine + 1]; 
char query[LimitRequestLine + 1]; 

bool parse(const char* line, char* abs_path, char* query) 
{ 
    char* method = "GET "; 
    char* valid_http = "HTTP/1.1"; 
    int index, method_size; 
    char abs_path_line[LimitRequestLine + 1]; 
    char query_line[LimitRequestLine + 1]; 
    int abs_path_index; 

    if(strstr(line, "HTTP/")!=NULL && strstr(line, valid_http) == NULL) { 
     error(505); 
     return false; 
    } 

    //make sure that our method is GET 
    for(index = 0, method_size = strlen(method); index<method_size; index++) { 
     if(line[index] != method[index]) { 
      error(405); 
      return false; 
     } 
    } 

    //check if request-target starts with '/' 
    if(line[index]!='/') { 
      error(501); 
      return false; 
    } 

    for(abs_path_index = 0; index < strlen(line); index++) { 

     //if there is a quotation mark, then we have a query in request-target 
     if(line[index] == '?') { 
     index++; 
     int query_index; 

     for(query_index = 0; line[index]!=' '; index++) { 

      //check if there is quote mark in query 
      if(line[index] == '"') { 
       error(400); 
       return false; 
      } 

      query_line[query_index] = line[index]; 
      query_index++; 
     } 

      query_line[query_index] = '\0'; 
     } 

     //assuming that we have not found any '?' mark for query. 
     if(strstr(line, "?") == NULL) { 
      query_line[0] = '\0'; 
     } 

     if(line[index] == ' ') { 

      int temp = index; 
      index++; 

      /*After the space there should be a valid http, if it is not found, 
      then there is/are spaces in our request-line which is incorrect*/ 
      for(int i=0; i<strlen(valid_http); i++) { 
       if(line[index] != valid_http[i]) { 
        error(400); 
        return false; 
       } 
       index++; 
      } 

      index = temp; 
      break; 
     } 

     //check if there is quote mark in abs_path 
     if(line[index] == '"') { 
      error(400); 
      return false; 
     } 

     abs_path_line[abs_path_index] += line[index]; 
     abs_path_index++; 
    } 

    abs_path_line[abs_path_index] += '\0'; 

    abs_path = abs_path_line; 
    abs_path += '\0'; 
    query = query_line; 
    printf("abs path is %s\n", abs_path); 
    printf("query is %s\n", query); 


    return true; 
} 

回答

1

問題是這樣的:

query = query_line; 

char *query意味着你傳遞一個指針。這只是一個像任何其他數字一樣的數字。這樣想想吧。

void set_number(int number) { 
    number = 6; 
} 

您是否期望這樣做?不。與query = query_line一樣的東西。

而是,query指向一大塊內存。您需要將query_line複製到query指向的內存中,並希望有足夠的分配空間。

strncpy(query, query_line, LimitRequestLine); 

需要調用者分配內存函數是一個等待發生的內存問題。我會建議...而不是修復這個...

  1. 寫一個更好的簽名,可能會返回一個結構的新函數。
  2. 將這個舊函數作爲新函數的包裝。
  3. 棄用此功能。

注意,在你的函數的query是不一樣的功能外宣佈query

+1

謝謝你的回覆!那正是我正在尋找的:) –