forked from azadkuh/qhttp
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
example/helloworld: update modes and README
- better printing of headers or message bodies ... - update README to show new changes
- Loading branch information
Showing
4 changed files
with
140 additions
and
150 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.