So I've been working on pset6 for a while now, the hardest thing with it is that it is very hard to test (I use a test.c file to do that and lots of printf functions), and the whole code of server.c is kind of confusing, I feel like I'm shooting in the dark sometimes.
Anyway, with that said, I think I completed "lookup" but cannot really test it as my code never gets there. When I run my code and try to go on the webpage I get 301 moved permanently, and on the page I get "too many redirects". From this I deduced that the problem must be with my abs_path and query pointers. A sad deduction because I spent days on doing parse.
Allow me to paste in my (most likely) faulty code and explain what I am trying to do with it:
bool parse(const char* line, char* abs_path, char* query)
{
// declaring arrays for holding all these strings (and feels)
char abpathholder[LimitRequestLine + 1];
char queryholder[LimitRequestLine + 1];
char httpholder[9];
memset(abpathholder, 0, LimitRequestLine + 1);
memset(queryholder, 0, LimitRequestLine + 1);
memset(httpholder, 0, 9);
// getting the start of absolut path and the start of HTTP version - 1
char* abpathstart = strchr(line, '/');
char* lastspace = strrchr(line, ' ');
char* querystart = strchr(line, '?');
// setting up an int to see where the path and query ends
int pathend;
int queryend;
// checks if there is a query and sets difference in strings accordingly, also gets the query
if (querystart == NULL)
{
pathend = strlen(abpathstart) - strlen(lastspace);
queryholder[0] = '\0';
}
else
{
pathend = strlen(abpathstart) - strlen(querystart);
queryend = strlen(querystart) - strlen(lastspace) - 1;
for ( int i = 0; i < queryend; i++)
{
queryholder[i] = querystart[i + 1];
}
}
// copies accurate number of chars for abpath
for (int i = 0; i < pathend; i++)
{
abpathholder[i] = abpathstart[i];
}
//for (int i = 0; i < strlen(abpathholder); i++)
// printf("This is in abpathholder: %c\n", abpathholder[i]);
for (int i = 1; i < strlen(lastspace); i++)
{
if (lastspace[i] == '\r')
{
break;
}
else
{
httpholder[i - 1] = lastspace[i];
}
}
//for (int i = 0; i < strlen(httpholder); i++)
// printf("This is in httpholder: %c\n", httpholder[i]);
// error checking begins here
// checking simply if line starts with GET
if (strncmp(line, "GET", 3) != 0)
{
error(405);
return false;
}
// cheking for any " in line
for (int i = 0; i < strlen(line); i++)
{
if (line[i] == '"')
{
error(400);
return false;
}
}
// checking if request-target really begiins with a /
if (abpathholder[0] != '/')
{
error(501);
return false;
}
// checking if HTTP version is correct
if (strcasecmp(httpholder, "HTTP/1.1") != 0)
{
error(505);
return false;
}
abs_path = abpathholder;
query = queryholder;
for (int i = 0; i < strlen(abs_path); i++)
printf("This is in abs_path: %c\n", abs_path[i]);
for (int i = 0; i < strlen(query); i++)
printf("This is in query: %c\n", query[i]);
printf("Reached true!\n");
return true;
}
So at first I'm just trying to see if line begins with GET or not. That's pretty straightforward.
Next I'm trying to get where the path starts, and where it ends.
I then calculate from the two, how long the path is
Then I am copying the appropriate number of characters into a new string, that will be later pointed at by abs_path - my idea is that the number of chars in the path will vary every time the client requests a new page
Next, I do almost the same with the HTTP version, but having an easier time, because I can just read up tp \r - this is only for later error checking (if HTTP version is correct)
I then check abstartpath (a string from the start of the absolute path to the end of line) if yes: I set a flag variable to true, if not, then not
If flag variable is true, I attempt to get the length of the query, and store it in a string, and immidiately point the query to it - if not true, I just declare an array of size 1 with \0 in it as per the specs
- After that is just a bunch of error checking (all passed, tested with printfs and such)
I'm not sure what goes wrong where. I have a feeling that maybe I shouldn't be sizing the arrays according to the size of abs_path and query, instead maybe just declare an array of LimitRequestLine + 1 and use that, but I'm not sure why my method is not working. Or maybe my whole logic is wrong. Dunno.
Any ideas?
EDIT:
So I have re-written my code to be a bit more clear, but I'm stil getting that darn 301 error (Moved permanently), with too many redirets in chrome, and not properly redirected in firefox.
So here the new walkthrough of my code:
Declaring char arrays to hold my strings and emptying them by memset
Getting vital parts of the string with strchr and strrchr
Declaring two ints to calculate the difference of string lenghts
Checking if there is a query, putting query in my queryholder array and calculating pathend and calculating pathend accordingly
With pathend I can calculate how long the path is, and then I put it in my final array for it (abpathholder)
Gwtting the HTTP variant of the client
Bunch of error checking (all passed)
Setting pointers to my arrays
And that should work, shouldn't it? Or am I misunderstanding the whole point of parse? (Oh don't mind the bunch of printfs on the way, it's just for me to see what's happening)