douzhun4124 2016-11-14 17:47
浏览 32

CS50 PSET6 server.c解析,加载垃圾内存,首先“GET”调用工作,但第二次404错误

I have been trying to figure out why one of my variables attaches some garbage value the second time I make a call in gdb but cannot figure it out. I have diagnosed the problem but don't know how to fix it.

On the first call of "curl http://localhost:8080/cat.html", here is what the variables return:

temp = 0x7fffffff9c74 "/cat.html HTTP/1.1
"

request_target = 0x607080 "/cat.html"

abs_path = 0x7fffffffbdd0 "/cat.html"

query = 0x7fffffff9dd0 ""

This works fine and the terminal does show the html file. However, the second time I make the call is where the problem arises.

On the second call of the same address, the following variables are returned:

temp = 0x7fffffff9c74 "/cat.html HTTP/1.1
"

request_target = 0x607080 "/cat.html\327\254\367\377\177"

abs_path = 0x7fffffffbdd0 "/cat.html\327\254\367\377\177"

query = 0x7fffffff9dd0 ""

And then I will receive the 404 file not found error.

Does anybody know why and where I am accumulating all these random characters ("\327\254\367\377\177")? I do believe that it is coming from the temp variable, but I have tried for hours to fix it but I still haven't been able to.

Below are my "parse" and "load" functions:

bool parse(const char* line, char* abs_path, char* query)
{
    // GET /cat.html?name="Alice" HTTP/1.1
    if (strncmp(line, "GET ", 4) != 0)
    {
        error(405);
        return false;
    }
    else if (line[4] != '/')
    {
        error(501);
        return false;
    }
    else if (strncmp(strrchr(line, ' '), " HTTP/1.1", 9) != 0)
    {
        error(505);
        return false;
    }
    char* temp = strchr(line, '/');
    int after_length = strlen(strrchr(line, ' '));
    char* request_target = malloc(strlen(temp) - after_length);
    strncpy(request_target, temp, strlen(temp) - after_length);
    if ((strchr(request_target, '"') != NULL || strchr(request_target, ' ') != NULL))
    {
        error(400);
        return false;
    }
    if (strchr(request_target, '?') == NULL)
    {
        strcpy(abs_path, request_target);
        strcpy(query, "");
    }
    else
    {
        int q_length = strlen(strchr(request_target, '?'));
        if (q_length != 2)
        {
            strncpy(abs_path, request_target, strlen(request_target) - q_length);
            strcpy(query, &strchr(request_target, '?')[1]);
        }
        else
        {
            strncpy(abs_path, request_target, strlen(request_target) - 2);
            strcpy(query, "");
        }
    }
    free(request_target);
    return true;
}

bool load(FILE* file, BYTE** content, size_t* length)
{
    if (file == NULL)
    {
        error(500);
        return false;
    }
    *content = NULL;
    *length = 0;
    BYTE buffer[BYTES];
    ssize_t bytes = fread(buffer, 1, sizeof(buffer), file);
    while (bytes != 0)
    {
        *content = realloc(*content, *length + bytes);
        memcpy(*content + *length, buffer, bytes);
        *length += bytes;
        bytes = fread(buffer, 1, sizeof(buffer), file);
    }
    return true;
}
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 谁有desed数据集呀
    • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
    • ¥15 关于#hadoop#的问题
    • ¥15 (标签-Python|关键词-socket)
    • ¥15 keil里为什么main.c定义的函数在it.c调用不了
    • ¥50 切换TabTip键盘的输入法
    • ¥15 可否在不同线程中调用封装数据库操作的类
    • ¥15 微带串馈天线阵列每个阵元宽度计算
    • ¥15 keil的map文件中Image component sizes各项意思
    • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏