0

I'm trying to do the first part of pset6, but i always get a core when try to set query = '\0' in the case that there's no question mark and when there's a question mark followed by space, or by q space strange things happens. I've coded outside the server.c, putting an expected string as test, so i can change the string to have no question mark, or a question mark followed by a q, a question mark followed by q space, or whatever, but yet after at least 15 hours coding, it never works...

    #include <stdio.h>
    #include <string.h>

    int main(void)
    {
        char line[] = "GET /cat.html?q=M HTTP/1.1";
        const char* met = line;
        char* SP = strstr(met, " ");
        char method[SP - met + 1];
        strncpy(method, met, SP - met);
        method[SP - met] = '\0';
        printf("%steste\n", method);
        const char* rtar = SP + 1;
        char* SP2 = strstr(rtar, " ");
        char rtarg[SP2 - SP];
        strncpy(rtarg, rtar, SP2 - SP - 1);
        rtarg[SP2 - SP - 1] = '\0';
        printf("%steste\n", rtarg);
        const char* htvr = SP2 + 1;
        int lenghthtv = strlen(htvr);
        char httver[lenghthtv];
        strcpy(httver, htvr);
        printf("%steste\n", httver);
        const char* qry = rtarg;
        char* QM = strstr(qry, "?");
        char* QMA = QM;
        char* qury = NULL;
        if (*(QM + 3) == ' ' || *(QM + 2) == ' ' || *(QM + 1) == ' ' || QM == NULL)
        {
            QM = SP2 - 4;
        }
        char query[SP2 - QM - 3];
        if (*(QMA + 3) == ' ' || *(QMA + 2) == ' ' || *(QMA + 1) == ' ' || QMA == NULL)
        {
            strncpy(query, qury, 1);
        }
        else
        {
            strncpy(query, QMA + 3, SP2 - QM - 3);
            query[SP2 - QMA - 4] = '\0';
        }
        printf("%steste\n", query);
    }

So, with this line, it runs as i expected, if i change the line to"GET /cat.html?q= HTTP/1.1", re-build, query has nothing, as expected, but if i change to "GET /cat.html?q HTTP/1.1" or "GET /cat.html? HTTP/1.1", query is not null, and if i change to "GET /cat.html HTTP/1.1", i'm core dumped. I've tried several others options, but it behaves the same, i'm guessing maybe it's because of the declaration of query, depending on the pointers SP2 (second space) and QM (question mark).

0

Ok fellas, here we go. Fisrt of all, i was trying to put in query whatever could be after the = sign, this is wrong, because the query shall contain q= also. Then i figured out that i could calculate the size of query using the strlen of the question mark and the strlen of 2nd space, so i've changed the last part of the code, and that worked (phew). Thank you for vieweing this.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .