Your second example makes it plain what is happening:
GET /cat.html HTTP/1.1
buffer = /cat.html
path = /cat.html
GET / HTTP/1.1
buffer = /cat.php
path = /cat.phpl
You are writing over the contents of the previous buffer without erasing them first. (Hence, the leftover "l" at the end of "/cat.phpl")
This is not supposed to happen.
It's hard to say exactly how to fix it without seeing any of your C source code, but two things spring to mind:
- Are you remembering to
free()
everything that you malloc()
?
- When you allocate dynamic memory for your
abs_path
and buffer
, have you tried using calloc()
instead of malloc()
, to zero out any garbage values remaining in the buffer?
EDIT: Well, there's your problem. You should use malloc()
(or better yet, calloc()
) and free()
for your buffer.
You should not mess with abs_path
, which as you say is out in main()
-- just declare a buffer in the function scope for parse()
, then allocate memory for it, build whatever string you need to build, copy that string to abs_path
, and free the buffer memory. (In my solution, I used separate buffers for abs_path
and query
and made sure to free them as soon as they were not needed.)
Sorry I can't reply in comments (because mobile.)