Skip to content

Commit

Permalink
example/helloworld: update modes and README
Browse files Browse the repository at this point in the history
- better printing of headers or message bodies ...
- update README to show new changes
  • Loading branch information
azadkuh committed May 31, 2016
1 parent c0e1aba commit af99ca6
Show file tree
Hide file tree
Showing 4 changed files with 140 additions and 150 deletions.
29 changes: 11 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,34 +59,28 @@ int main(int argc, char** argv) {
using namespace qhttp::client;
QHttpClient client(&app);
QByteArray httpBody;
QUrl weatherUrl("http://api.openweathermap.org/data/2.5/weather?q=tehran,ir&units=metric&mode=xml");
QUrl weatherUrl("http://wttr.in/tehran");
client.request(qhttp::EHTTP_GET, weatherUrl, [&httpBody](QHttpResponse* res) {
client.request(qhttp::EHTTP_GET, weatherUrl, [](QHttpResponse* res) {
// response handler, called when the HTTP headers of the response are ready
// gather HTTP response data
res->onData([&httpBody](const QByteArray& chunk) {
httpBody.append(chunk);
});
res->collectData();
// called when all data in HTTP response have been read.
res->onEnd([&httpBody]() {
// print the XML body of the response
puts("\n[incoming response:]");
puts(httpBody.constData());
puts("\n\n");
res->onEnd([req]() {
// save req->collectedData() (as html body) to a file or ...
QCoreApplication::instance()->quit();
});
// just for fun! print incoming headers:
puts("\n[Headers:]");
const qhttp::THeaderHash& hs = res->headers();
for ( auto cit = hs.constBegin(); cit != hs.constEnd(); cit++) {
printf("%s : %s\n", cit.key().constData(), cit.value().constData());
}
// just for fun! print headers:
qDebug("\n[Headers:]");
const auto& hs = res->headers();
qhttp::for_each(hs.constBegin(), hs.constEnd(), [](auto cit){
qDebug("%s : %s", cit.key().constData(), cit.value().constData());
});
});
// set a timeout for making the request
Expand All @@ -95,7 +89,6 @@ int main(int argc, char** argv) {
QCoreApplication::quit();
});
return app.exec();
}
```
Expand Down
107 changes: 32 additions & 75 deletions example/helloworld/README.md
Original file line number Diff line number Diff line change
@@ -1,103 +1,60 @@
# helloworld

this example shows the usage of `QHttp` as an HTTP server or a client. you can try this by TCP socket or unix (local) sockets.
this example shows the usage of `QHttp` as an HTTP server or a client.

##usage

```bash
$> ./helloworld -h
```

```text
Usage: ./helloworld [options] mode
a Hello World sample for http client and server.
Options:
-h, --help Displays this help.
-v, --version Displays version information.
-b, --backend <type> backend type of HTTP. could be 'tcp' or 'local',
default: tcp
-p, --port <number> tcp listening port, default: 8080

Arguments:
mode working mode, client or server. default: server
#Usage:
$> ./helloworld {mode} [options]
```

##TCP sockets
three different modes are available:

###tcp server
to test `QHttp` server classes, start `helloworld` in server mode:
```bash
$> ./helloworld server -p 7000
```
now the server is listening on `address=QHostAddress::Any`, `port=7000` (press `ctrl+c` to stop the server).
### server

<br/>
then test the server by pointing your browser to [localhost:7000](localhost:7000) or try any other HTTP client app:
to start a new http server:
```bash
$> curl localhost:7000
$> ./helloworld server --listen 8080
```
you shall see the `Hello World!` message from the server.

to stop the server just press the `ctrl+c` or send a `command: quit` as an http
header to this server (by an http client).

###tcp client
the simple client mode tries to fetch the weather information from [openweathermap.org](openweathermap.org):
sample clients:
```bash
$> ./helloworld client
```
you shall see the headers:
```
access-control-allow-credentials : true
access-control-allow-methods : get, post
transfer-encoding : chunked
server : nginx
content-type : text/xml; charset=utf-8
access-control-allow-origin : *
date : sun, 20 jul 2014 09:21:19 gmt
connection : close
x-source : redis
```
# GET by curl
$> curl 127.0.0.1:8080/login/?username=admin
# send an http header to stop the server
$> curl -H "command: quit" 127.0.0.1:8080

# POST by curl, custom headers, custom body (data)
$> curl -X POST -H "my_key: my_value" -H "connection: close" \
-d "this is http body of POST request" \
127.0.0.1:8080/path/?cmd=have_fun

and XML body as:
```xml
<?xml version="1.0" encoding="utf-8"?>
<current>
<city id="112931" name="Tehran">
<coord lon="51.42" lat="35.69"/>
<country>IR</country>
<sun rise="2014-07-20T01:33:35" set="2014-07-20T15:47:49"/>
</city>
<temperature value="38" min="38" max="38" unit="celsius"/>
<humidity value="8" unit="%"/>
<pressure value="1006" unit="hPa"/>
<wind>
<speed value="6.2" name="Moderate breeze"/>
<direction value="140" code="SE" name="SouthEast"/>
</wind>
<clouds value="20" name="few clouds"/>
<visibility/>
<precipitation mode="no"/>
<weather number="801" value="few clouds" icon="02d"/>
<lastupdate value="2014-07-20T09:00:00"/>
</current>
```

##Unix socket
to test unix (local) sockets as backend, use `-b local` option.

##local server
do:
### client
to fetch a custom http url:
```bash
$> ./helloworld server -b local
$> ./helloworld client --url https://www.google.com/?gws_rd=ssl#q=qt+c%2B%2B11
$> ./helloworld client --url 127.0.0.1:8080
```
now the HTTP server listens on `/tmp/helloworld.socket` and says `Hello World!` to connected clients as:

the client mode dumps both headers and body data.


### weather
to fetch the weather information of your favorite city (by awesome
[wttr.in](http://wttr.in) service):

###local client
do:
```bash
$> ./helloworld client -b local
$> ./helloworld weather --geolocation Tehran
$> ./helloworld weather --geolocation Paris,Fr
```

now you shall see the server message and HTTP headers.
the result will be save into `weather.html` file, you can open it by a browser.

2 changes: 1 addition & 1 deletion example/helloworld/helloworld.pro
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ osx:CONFIG -= app_bundle

TARGET = helloworld
TEMPLATE = app
CONFIG += c++11
CONFIG += c++14

PRJDIR = ../..
include($$PRJDIR/commondir.pri)
Expand Down
Loading

0 comments on commit af99ca6

Please sign in to comment.