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;
}
謝謝你的回覆!那正是我正在尋找的:) –