I've read the explanation and specification for parse, but I still do not know what I am supposed to do for parse. I've looked at other questions, but I am still confused about the objectives of parse. I am also confused about abs_path and query. What exactly are these values representing?

Thanks in advance.


The two main tasks for parse() are to separate out the "absolute path" string value and the "query" string value. You are passed in a string, which is the line that was given by the browser to the web server. You are also passed pointers to two strings "abs_path" and "query" into which you will copy your found strings. That's the basics. The subtle points are handling the situation where there is no query string, or the request line itself is invalid. The instructions mention functions like strchr() and strstr() which can help you "chop up" the input string into the other parts. strchr() for example will look for a particular character (like a space) in the string, and return a pointer to it if it is found. You might attack it like this:

  • Make a complete copy of the line
  • Use strtok() to chop that up, and get 3 pointers (to the method, the request, and the http version string)
  • Use strtok() again on the request, to chop it up into the path and the query string (if present)
  • Do all your checks, returning error codes if needed
  • copy out the found strings into the strings we were passed

That's the strtok() approach. Or you could use a strchr() approach which might look like this:

  • make some new strings to hold the method, the request, and the http version string
  • make sure there are exactly two spaces in the line, so that there are 3 things in this line
  • Copy everything up to the first space into the method
  • Copy everything from there to the next space into the request
  • Copy everything else into the http version string
  • Now make a new empty string for the query; see if there is a question mark in the request; if so, copy out the query
  • Do all your checks
  • copy out the found strings into the strings we were passed

You must log in to answer this question.

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