0

Updated my parse and load now. I am dying to find out why 301 error now:( Must be happening in redirect function.

 typedef char BYTE;
 #define LimitRequestLine 8190

bool parse(const char* line, char* abs_path, char* query) {

//abs_path start pointer
char* array = malloc(LimitRequestLine + 1);
//creating a method string together with a NULL terminator
char* method = NULL;
method = realloc(method,sizeof(char));
method[strlen(method)] = '\0';
//creating an array to check for '\'

//array that will be a buffer for an abs_path
char array3[LimitRequestLine + 1];
char array4[LimitRequestLine + 1];
//pointer for HTTP-version
char* http = malloc(sizeof(LimitRequestLine + 1));
char* check =malloc(sizeof(LimitRequestLine + 1));

//copying method into a pointer method and checking whether it matches "GET"
char* pastMetEnd = NULL;
pastMetEnd = realloc(pastMetEnd,sizeof(BYTE));
pastMetEnd = strchr(line,' ');
strncpy(method,line,(strlen(line) - strlen(pastMetEnd)));
method[strlen(method) + 1] = '\0';
// printf("%s",method);
//check for '/' in front of the abs_path
int num = (strlen(line) - strlen(pastMetEnd))+1;
//check for '/' in front of the abs_path
if(line[num]!=47)
{
    error(501);
    return false;
}
//checking for GET
printf("%s",method);
if(strcmp(method,"GET") != 0)
{
  printf("dupa");
   error(405);

   return false;
}
//getting the start of http ver
check = strchr(line,'/');
http = strchr(check,' ');
//checking for the correct http ver
if(strcmp(http," HTTP/1.1\r\n")!=0 )
{
    printf("lel");
    error(505);
    return false;
}
//getting the start of an abs_path
array = (strchr(line,'/'));
// populating a abs_path buffer
for(int j = 0;array[j]!= 'q' && array[j] != ' '  ; j++)
{

    array3[j] = array[j];

}
//check for " " or ' " ' inside of abs_path buffer
for(int z = 0; z < strlen(array3);z++)
{
   // printf("%c",array3[z]);
    if(array3[z] == 32 || array3[z] == 34)
    {
        error(400);
        return false;
    }
}

//populating abs_path
abs_path=array3;
abs_path[strlen(abs_path)+1] = '\0';
printf("%s", abs_path);
//getting array to point to the start of the querry
if(strchr(line,'q') != NULL){
array = strchr(line,'q');
for(int i = 0; array[i] != 32; i++)
{
    array4[i] = array[i];
}
query = array4;

printf("%s" , abs_path);

for(int z = 0; z < strlen(array4);z++)
{

    if(array4[z] == 32 || array4[z] == 34)
    {
        error(400);
        return false;
    }
}
}
else
{
    *query = ' ';

}
query[strlen(query)+1] = '\0';
// printf("%s" , query);


//error(501);
return true;

} } bool checkEnd(const char* droga, char* end) { //char* temp = malloc(strlen(end + 1)); char* temp = strrchr(droga,end[0]); if(strcmp(end,temp) == 0) {

    return true;
}
//free(temp);
return false;

}

 const char* lookup(const char* path)
{
//creating arrays of endings and mime's
char* array1[8] = {".css", ".html", ".gif", ".ico", ".jpg", ".js", ".php", ".png"};
char* array2[8] = {"text/css", "text/html", "image/gif", "image/x-icon", "image/jpg"
                  ,"text/javascript", "text/x-php", "image/png"};
//checking if path contains the ending from array1 and if so returning array2 equivalent
for(int i = 0; i < 8; i++)
{
    if(checkEnd(path,array1[i]))
    {
        return array2[i];
    }

}
return "No such file available";

}

bool load(FILE* file, BYTE** content, size_t* length)

{

if(file == NULL)
{
    printf("File is empty and therefore can not be read!!");
    error(404);
    return false;
}

BYTE array[sizeof(int)];
BYTE* storedBytes = NULL; 
storedBytes = realloc(storedBytes,sizeof(BYTE));
int i = 0;
//load all of the bytes into and array and count the bytes
while(fread(array,sizeof(BYTE),1,file))
{
     storedBytes[i] = array[0];
    *length = *length + 1;
     i++;   


}

*content = storedBytes;
//printf("%d", (int)*length);
//printf("\n%s", storedBytes);

return true;

}Now indexes

   char* indexes( char* path)
{
char* address = malloc(100));
//setting up a buffer to hold that path
char array[strlen(path) + 11];
char* indexes[2] = {"index.php","index.html"};
//poplating first buffer
for(int i = 0; i < strlen(path);i++)
{
  array[i] = path[i];
}
//checking for a match

for(int n = 0; n < 2; n++){    

    strcat(array,indexes[n]);
    address = array;
    FILE* fp = fopen(address,"r");

    if(fp != NULL )
    {
        printf("FOUND");
        return address;
    }
    else
    {
        for(int i = 0; i < strlen(array);i++)
        {
            array[i] = path[i];
        }
    }
}
//if no match return NULL

return NULL;
0

Start with indexes. The initial call to the website will call indexes. This char* address = malloc(sizeof(100)); is a problem. 100 is an int. size of an int is 4. I did not read any further in the function. You can take indexes out of the equation by sending a browser or curl request for hello.html. But I think that will only return a "<" (if anything) because...

load has serious structural defects.

  1. BYTE array[LimitRequestLine * 2]; you are only ever reading 1 byte into array. Why allocate 8190 * 2 bytes? (that's not technically a defect, just a waste of good memory)
  2. BYTE storedBytes[LimitRequestLine * 2];. cat.jpg is 26860 bytes. It ain't gonna fit. load really requires a realloc.
  3. Isn't storedBytes[0] a single char? Won't that make content really wrong?

I didn't really look at parse past this char* http = malloc(sizeof(LimitRequestLine + 1)); is a problem (for the same reason stated above).

You will need to get cozy with debug50 or gdb (tips here) and valgrind. Sometimes a simple curl request to hello.html is easier to debug than a browswer call to the server.

And spoiler alert!, char* array2[8] in lookup does not match the spec.

| improve this answer | |
1

I've gotten ECONNRESET as well, never was much of an issue though. I'm not going to look through the code as I am still on the pset myself.

Though you may want to try "deactivating" every function by returning its equvialent of "false" immediately, gradually "enabling" them again, and see where/when the ECONNRESET message appears. You may also use gdb from the terminal (what I did), and run it without breaking the program anywhere. Now gdb should print an error message with the line number and a cause (I got SIGABRT exclusively). I don't know what causes SIGABRT, but while you're in gdb you might as well want to inspect some memory, and Google probable causes of SIGABRT.

Hopefully this will at least give you an insight to your problem.

| improve this answer | |
  • Going to look into it now:) Thanks – Kamil Kug Sep 22 '16 at 21:42
  • Tried to do that but can not check inside of while loop for some reason , I mean using gdb. I am getting a blank screen every time I press n (I mean in terminal). But I think that the problem is inside of parse somewhere:) Thanks a lot for some advice, have to go sleep now as work early in the morning hehe. – Kamil Kug Sep 22 '16 at 21:57
  • So I am trying to go into any of the functions in my server.c using gdb. Going step by step even. But gdb does not seem to want to show what is happening in if(connected). I really think I am useless at the moment and feel like I want to give up hehe. Especially after prof Malan said that pset5 was so hard:D I managed to do pset5 two different ways, but this pset6 seems so overhelming haha. BTW updating my code a bit. – Kamil Kug Sep 23 '16 at 15:51
  • If you remember when you got ECONNRESET the first time it's probably a good idea to check there. You can even save your current code inside a separate file and revert your code back to an older version by clicking "File" in the top-right corner then "Show revision history" or see here, and then you can proceed from there. I'm at the end of the indexes() so I'll look over your code when I pass check50. – Martin Kleiven Sep 23 '16 at 16:31
  • Thanks a lot man. I will carry on working on it and hopefully will come out with some solution soon:) – Kamil Kug Sep 23 '16 at 19:12

You must log in to answer this question.

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