Click here to Skip to main content
15,881,281 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello all

I have a C++ project under Linux, the program will receive Xml content through HTTP requests. I have already planned for XML content parsing by using the TinyXML-2 library, I'm stuck at the point on how to receive HTTP requests. Any advice on how to handle HTTP requests that are sent to my program ?

Update:
The HTTP request i'm receiving is content-type: text/xml.
Posted
Updated 24-Nov-14 22:42pm
v2

One good thing about being a special purpose HTTP server is that you don't have to process the entire protocol and you can just handle the bits you want and send an error message for just about anything else. While lbcurl can help it's mostly for HTTP clients, not servers so I wouldn't bother with it. Another option might be to write an Apache handler to do what you want but that's overkill compared to a simple server.

So listen on port 80, wait for a connection and then you'll get a lump of data from your client. Hack off the headers (they're the block of text at the start of the lump of data), process the ones you need (content length and mime type), chuck the rest in a buffer and let tinyXML do its stuff. Finally send a 200 OK with a body or a 4xx response describing what happened to your client.

Grab a copy of HTTP: The Definitive Guide from Amazon or your local library and you'll find out all you need to know and then some more.
 
Share this answer
 
Comments
Abdallah Al-Dalleh 26-Nov-14 2:19am    
If I'm using an existing web server, how to handle http requests through the web server and not through custom handling through port 80 ?
Aescleal 26-Nov-14 5:19am    
I can't really comment on this as I haven't done it in nearly 20 years. Writing a CGI program might be a good way to go if you can work out how to do it. Your XML stream will effectively come in as either a command line argument or stdin. You might have to relax security on your webserver though as I can't imagine most webservers want arbitrary code to be run on the host.
Abdallah Al-Dalleh 26-Nov-14 14:26pm    
Thanks for the efforts man, no problem at all. Fortunately, today I solved it with a code snippet I found on the internet. I will write an Tip/Trick tomorrow about it.
hi,
there were many search results on searching "c++ handling http requests" on Google.
You can start by going through this one[^].
There are other open source libraries[^] which can help you with this task.

More libcurl examples are here.[^]

hope this helps!
 
Share this answer
 
Comments
Abdallah Al-Dalleh 25-Nov-14 6:42am    
I tried neon and happyhttp. I'm having build problems in both.
In neon, I downloaded the package and installed it on my system, I include it correctly but the compiler says undefined reference.
I downloaded happyhttp and include it in my project but there are some functions that are undefined :\
chandanadhikari 26-Nov-14 1:51am    
i have not tried neon and happyhttp but the 'undefined reference' means that the compiler is still not able to find it, so its not being included correctly.If you are using an IDE then just check that the settings you gave are correct.
I would suggest you try with libcurl.
Start by building the sample code snippets that are provided in the link.
Abdallah Al-Dalleh 26-Nov-14 2:19am    
If I'm using an existing web server, how to handle http requests through the web server and not through custom handling through port 80 ?
chandanadhikari 26-Nov-14 4:48am    
i hope google will be able to help you with this . try searching for this with your web server's name.
chandanadhikari 27-Nov-14 4:38am    
in case you plan to go for CGI a helpful link is here:
http://www.tutorialspoint.com/cplusplus/cpp_web_programming.htm
Please do share your solution -thanks
For anyone facing this problem, see this:

Capture Incoming HTTP Requests to the Web Server[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900