Making HTTP request from an apache2 module (c++) - c++

My apache2 module written in c++ works just fine, it handles "page.xyz"-like requests from browser clients, and it can return the appropriate result.
What I need now is to use my module as a client to another server: make a HTTP (GET) request and get the response (GET https://graph.facebook.com/oauth/access_token?...).
Does apache has a magic can do this, or do I have to deal with sockets and make HTTP packets manually? What is the best way to do this?
Many thanks!

I am guessing you are on your own with the HTTP client. Probably the easiest way to make an HTTP connection is to use libcurl. On linux it should be installable from your distro's repositories.

Related

Configure clojure http-kit to use a forward proxy

I have a little Clojure app that uses http-kit to send some http post requests to a server. I want to route the https POST request through a proxy P, ie. I want the traffic to go like App->Proxy->Server.
(This is because the target host X restricts access based on IP)
Is this possible?
Also the App runs on an ubuntu server, are there maybe system-level configurations possible to make http-kit use a proxy server? I prefer other processes to be unaffected though.
http-kit is supposed to follow the standard method of configuring proxies in Java:
-Dhttp.proxyHost=proxyhostURL \
-Dhttp.proxyPort=proxyPortNumber \
-Dhttp.proxyUser=someUserName \
-Dhttp.proxyPassword=somePassword
which you can set in your lein profile or in the application server if you are using one.
http-clj now support proxy: https://github.com/dakrone/clj-http#proxies
For http-kit, according to the author's reply in this issue, the answer is NO.
But the good news is fewer weeks before it support basic HTTP proxy ( commit a207537 on http-kit ).
After all, it seems there is no way to set up a system wide proxy for JVM applications.

How to make Qt Websocket and QNetworkRequest (HTTP) to use the same connection?

Is it possible with Qt to upgrade a HTTP connection that handles the normal HTTP requests to a Websocket with the same connection?
I'm thinking about something like this with Poco libraries, but all done in Qt similar to QtWebApp.
The simple answer is no and that is mostly because of specifics of the server side. And Qt just follows the protocol available and exposed by the server (HTTP/WebSocket) as mostly the client-side development framework and AFAIK won't be able to do the kind of transformation you want of going from HTTP to Websocket that are two different protocols. But of course, theoretically that can be done as long as both protocols able to use IP port 80. But that implies new unique sever and new unique client implementations.
We use both WebSocket and REST in our app. And WebSocket is for triggering the client by the server to do something. Client gets the "poke" from the server and starts normal JSON HTTP-based exchange with the server.
Somewhat relative link: https://softwareengineering.stackexchange.com/questions/276253/mixing-rest-and-websocket-in-the-same-api

c++ how to listen HTTP requests

Im new in C++.
I need to listen HTTP requests.
Please advice me some good tutorials or examples
Thanks
update:
Platform: Windows
Language: C++
I will explain more clearly what i need
when user clicks row on this page: http://ucp-anticheat.org/monitor.html applications is automatically starts on client machine.
I want to make same thing.
I think on client side is service which listens http requests and if url starts with steam:// service automatically runs application...
Do i need to listen http requests?
What is best solution for my problem?
You can listen to http requests through a web server like mongoose , which can be easily used in C++ http://code.google.com/p/mongoose/ , and here is a good example of using mongoose web server http://code.google.com/p/mongoose/source/browse/examples/hello.c
I m not sure what you mean 'client side', if you are meaning Browser as your client, you can't control nothing outside your browser. If you want to control a machine, you need your client machine to run your exe, that has the code to act based on your server instructions.
You should create a simple server program, create a SOCKET listening on default http, https etc, ports. Usually we do it inside a loop (at each one you make a read).
Now... would be easer if you specified if you are on Unix like OS or Windows, but from now on you can google it. Like sys/socket.h or try "man 7 socket" on almost all linux (at least the ones I know).
If you want to sniff something you can google some specific apps around web.
If i get your question right, you want to be able to launch an application when someone clicks a link with a custom protocol, like steam:// or telnet://. You are looking for an Protocol Handler.
A simple way to register such an application is using the ftype program, as described here.

Upload a file to a web server using C++

I want to upload files from a client location to a server. At present, I have a client-server socket program by which I could send/receive files across, but I would like to improvise it.
My idea would be to transfer the file using HTTP PUT/POST from client (most of the coding on client side) to the server. Since I have no idea about HTTP programming, so I need some guidance on how to achieve that. I want to use C++ with BSD sockets in doing that, and no other libraries. My aim is to send the server a form, like as given below with a HTTP POST/PUT request, and get the file "main.cpp" uploaded to the server.
PUT http://localhost/ HTTP/1.0
Host: localhost
Content-type: form-data
Content-length: 90
FileUpload: /Users/SG/files/main.cpp
I was able to write a dummy program that does some PUT from a client, and the web server running Apache returns a HTTP 200. What I am failing to understand currently would be the following two things, which I guess are somewhat connected:
How one could specify a file to be uploaded from the client in the form above?
If I understand correctly, the file would be read at client site and then the file content would be sent to the server, where a server side script would read the bytes from client and create a copy of the file at the server. Is it correct?
Any help is appreciated.
Thanks,
Sayan
As stated it's not possible. C++ alone has no sockets API. (Edit: with the addition of a BSD sockets API, it's now possible).
Your implementation might provide an OS-specific sockets API, in which case your question boils down to, "how do I write an HTTP client?". To which the answer is (a) don't, or (b) read the HTTP specification very carefully and do as it tells you. The basic steps are:
(Possibly) parse a URL and use gethostbyname to get an IP address.
Open a TCP socket, using connect.
Write the request (you already have an idea what that looks like) using send.
Read the response using read.
Close the connection.
The difficult part is parsing the response robustly, because there are a lot of features to worry about. If your client is tied to a particular server, then its responses will be quite predictable, and you can ignore quite a lot of the HTTP spec (at least until you change the configuration of the server, update its software, etc).
If you're ready to give up before you finish, there are perfectly good HTTP libraries available, such as libcURL.
I want to use C++ for doing this,
without the help of any libraries or
system() calls.
Socket programming always requires system calls. C++ is an extremely general language. The ISO language definition does not specify anything about sockets or network programming, so in order to do this you need to rely on another system-specific library/standard, such as the BSD socket API on UNIX/Linux. Usually, in C++ you would use a higher-level third-party library that wraps these lower level system calls, such as Boost.ASIO. However, even that takes a bit of learning, to say nothing of correctly implementing the HTTP standard itself on top of your sockets.
I was able to attain what I wanted. My http post request string looks like below:
"POST %s HTTP/1.0\r\n"
"Host: %s\r\n"
"Content-type: multipart/form-data\r\n"
"Content-length: %d\r\n\r\n"
"Content-Disposition: %s; filename: %s\n"
I am sending the above string from a cpp program in client to the server, running Apache, which handles the data through a cgi script, also written in cpp. The data is placed with the Content Disposition attribute. Can anybody please point it out to me as to how would I transform the above to a HTTP PUT?

Are there any CURL alternatives for C++?

I hate CURL it is too bulky with too many dependencies when all I need to do is quickly open a URL. I don't even need to retrieve the contents of the web page, I just need to make the GET HTTP request to the server.
What's the most minimal way I can do this and don't say CURL !##$
There are lots of choices! Try libwww -- but be warned, most people strongly prefer libcurl. It's much easier to use.
There's a very light way and I've done this myself when implementing a high-scale back end service for a large media provider in the UK.
This method is extremely operating-system specific.
open a TCP socket to the HTTP server
send a "GET /path/to/url HTTP/1.1\r\nHost: www.host.com\r\n\r\n" (the Host header is required for HTTP/1.1 and most virtual servers, don't forget the two blank lines, and a newline requires a carriage return as well for HTTP headers)
wait for the response
close the socket
If you are going to close the socket at the end of the connection you may also want to send a Connection: close\r\n as part of your headers to inform the web server that you will terminate the connection after retrieving the web page.
You may run into trouble if you're fetching an encoded or generated web page in which case you'll have to add logic to interpret the fetched data.
On Windows (Windows XP, Windows 2000 Professional with SP3 and above) you could use WinHttpReadData API. There's also an example at the bottom of that page.
More info on Windows HTTP Services on MSDN
I have used Winsock when I need as few dependencies as possible and it has worked well. You need to write more code than using a separate library or the Microsoft WinHTTP library.
The functions you need are WSAStartup, socket, connect, send, recv, closesocket and WSACleanup.
See sample code for the send function.
system("wget -q -O file.htm http://url.com");