Search code examples
c++httpsocketsparsingserver

Should i use std::string in a web server for parsing a client request?


I'm coding a little HTTP 1.1 web server in C++98 (c++ version mandated by my school) and i haven't make a decision about which data type i'm gonna use to perform the request parsing, and how.

Since i'll be receiving read-only (by read-only i mean that i don't have to modify the buffer) data from a user-agent, would it make sense to use std::string to store the incoming data ?

  • HTTP syntax is very straightforward, and can be parse using a finite state machine. Iterating over a const char * seems enough and doesn't make any allocations, i can use buffer that recv gives me.

  • On the other hand, i could use std::string facilities like find and substr to parse the request, but that would lead to memory allocations.

My server doesn't need to be as efficient as nginx, but i'm still worried about the performance of my application.

I'm eager to know your thoughts.


Solution

  • Definitely. It's a school project, not a high-performance production server (in which case you'd be using a more modern C++ variant).

    The biggest performance problem you'd typically have with std::string is not parsing, but string building. a + b + c + d + e can be rather inefficient. Details, really: just start by writing a correct implementation, and then see which parts are too slow in testing. There are very few projects, even in commercial software development, where that's not the right approach.