2

I'm coding a httpserver and I'm stuck at parsing GET requests. How would I parse a char buffer with something like

GET /images/logo.png HTTP/1.1

So that I only get the path and file extension but ignore the other parts?

5
  • std::string & std::string::substr Commented Aug 13, 2011 at 0:18
  • How would I use substr to get that certain portion? I can set a start value but the end part is never certain. Commented Aug 13, 2011 at 0:19
  • 2
    Have you read the specs? You can find them here: w3.org/Protocols/rfc2616/rfc2616-sec5.html#sec5.1 and you'll see what are the parts of the request line and how they're delimited... Commented Aug 13, 2011 at 0:23
  • the HTTP1.1 protocol requires the browser to include a hostname and the hostname wont always be the same sadly. Commented Aug 13, 2011 at 0:25
  • 2
    Without having set out to be nasty, if you can't parse this string perhaps an HTTP server is not the best project for you at this stage! Commented Aug 13, 2011 at 0:31

1 Answer 1

2

You don't say specifically say what sort of storage this string is in - simple char* or some string class.

So, in general, you could either do it the rather simple and dirty way, by splitting the string on the space character, and taking the second or middle section. Or, a better approach would be to get familiar with Regular Expressions. C++ has several regex libraries - Boost is well regarded.

Sign up to request clarification or add additional context in comments.

9 Comments

It is stored in a char array with the size of 2042 bytes. Is there any simple way to do this without using a library?
Yes, you could iterate through the char array, and only pull out characters between the first and second space into a second array. I would recommend using regex's however - it would then just become a call to match the input string against a pattern - and the library does all the hard work.
You need to be careful with URL encoding too - how are spaces encoded in this string, '%20' or similar?
@Kraffs : VC++ 2010 comes with an implementation of C++0x's regex library, built-in -- see here for docs. Alternatively, you could use Boost.Xpressive, which is header-only (no building/linking required) unlike Boost.Regex.
strtok is the devil's function.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.