Home > Enterprise >  Simplest way to process HTTP post data in C
Simplest way to process HTTP post data in C

Time:06-21

I would like a website to send data to be stored to a database. Consequently I think I require a web server, listening to HTTP, parsing the POST, extracting the data and storing to database?

I'd like the web server to be C .

However, I am struggling to find a simple example. I assume this is simple because we're just listening to a TCP socket and once all the data has been received, it's just a string parsing exercise? All the examples I can find seem way-over complicated. For example:

https://www.boost.org/doc/libs/1_72_0/libs/beast/example/http/server/async/http_server_async.cpp

Surely it shouldn't require 300-400 lines just to listen to a socket and parse a string?

Is my understanding wrong/is there a simple way to process a HTTP POST in C ?

CodePudding user response:

Writing your own web server in C that

  • only supports a single HTTP connection from a single client at a time,
  • assumes that the incoming HTTP requests are never malformed and always in a certain format, and always of a certain type,
  • in the case of something unexpected happening, is not required to provide a proper HTTP response,

would not be too hard. However, if you want your web server to be fully HTTP/1, HTTP/2 or HTTP/3 compliant, then writing such a server would be a lot of work, as those standards are dozens or even hundreds of pages long.

It would probably be more meaningful to use existing HTTP server software, such as Apache HTTP Server, and write your own programs that are designed to serve individual HTTP requests, and to configure the server to call these programs for certain URLs. That way, you won't have to deal with most of the details of HTTP, but you will still have to deal with HTTP requests and HTTP responses.

Some scripting languages such as PHP (which is designed to run on a web server) handle most of the remaining HTTP details for you, so you for example don't have to worry much about interpreting the HTTP request header and creating a HTTP response header. These scripting languages generally also allow you to connect to an external database server.

If you don't want to use a scripting language, but would prefer for the individual HTTP requests to be handled by programs written in C , then this is possible too, for example using CGI or an API provided by the HTTP server software.

  • Related