this is just a log to track my progress and problems i face from inception of project
- Could not for the life of me, assign the
HttpRequest
fields (requestUrl & httpMethod) to the return value ofstrtok(methodlinebuffer, " ")
-
originally had a
char *tmp
that imalloc()
and assigned the return value tostrtok(methodlinebuffer, " ")
, then, i'd have theHttpRequest
fields assigned to tmp.
the issue here was that theHttpRequest
fields was pointing to the memory address of tmp and seeing as I had tofree()
tmp, the HttpRequest fields would end up pointing to an unused memory location. -
that was not the only issue as when I initialized the
HttpRequest
struct, I did not initialize it as a Pointer but as a simple struct.
this meant that I could not passHttpRequest
by reference into a called function. To remedy this, I had a few options.- Set all functions that required altering
HttpRequest
to acceptHttpRequest *
as a parameter. Then in the callee function, I would pass&HttpRequest
or the memory address of it into The function. - Set functions to accept
HttpRequest *
and also initializedHttpRequest
struct as a pointer.
- Set all functions that required altering
as viewable in the repo, the remedy I chose ended up being number 2. Only after deciding to initialize HttpRequest
as HttpRequest *
and continued on writing more code that I realized that
there was another solution. THe justification for using method 2, is that I just got lazy to refactor and change, perhaps one of my todo(s)?
- hindsight is 20/20, I realized that having to allocate memory to
HttpRequest
is probably bad practice seeing as the size is known during compile time, will fix to initialize as a struct and pass in address of, into any functions that require to alterHttpRequest
.
- Minor issue with server crashing due to heap corruption error.
-
used the giga virgin method of debugging by printf-ing the memory addresses of every pointer malloc'd.
found out that there was a problem with thePATH_BYTE_SIZE
global variable. It represents the number of bytes that my "path/to/file.file" would have, in this case I believe that the minimum path byte size is 15 Bytes, "page/index.html". I set it to be able to receive atleast double that length so it was assigned to 30. The corruption was always 20 memory places or 20 bytes away from the first byte thatpath
points to, I could not for the life of me figure out why this was an issue despite being able to read up to double the byte size. I ended up increasing it to 45, or triplethe minimum byte size, the corruption issue went away. -
A possible explanation could be that I had an off by one error as I did not take into account the terminating character byte, thus making it a minimum of 16 bytes.
- Made a branch that could handle icons and image requesting! Also wanted to delve into a bit of handling http request properly.
Heres some things I learned.
- Reading binary to send over images through sockets.
- the strings library functions work by defining that a string is a sequence that ends with a NUL terminating special character, this means that trying to read information like using
strlen()
on a character pointer that holds binary data tends to lead to whacky undefinied behaviour - this also means that working with
strcpy()
orstrcat()
becomes kinda tough, through this I learned aboutmemcpy()
and copying memory directly onto memory addresses and how pointer arithmetic is helpful. - trying to get the icon to load on browsers was pretty funky, had a minor issue with browser hard caching the icon image, learnt and understand more about caching and that due to caching, a browser only sends requests for images when a new image that the browser has never seen before is needed.
- had an issue with memory heap corruption leading to undefinied behaviour where the server would be able to refresh twice or up to 20 times only. ended up finding out that compiling regex
needs to be freed with
regfree()
, however that was not the main issue, printed out the memory addresses of all pointers allocated and found out that the memory corruption occurs almost always about 16 bytes away from HttpRequest, thought it could be an issue with the way I was hanling HttpRequest, decided to change it up to only pass the address of HttpRequest into functions that need to alter it, didnt solve the issue but it did move the corrupted memory to a specific location very nearby the HttpResponse pointer. It ended up being a silly mistake where I forgot to dereference the fileLength variable in thegenerate_http_response()
function, that was a frustrating but oddly fun issue to have to handle. -learned about unsigned char use cases, difference betweeen uint8_t and the rest and how unsigned char is much better suited for storing byte data.