Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
M
mongoose
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Ganil-acq
GANILinux
linux-service
library
mongoose
Commits
9fe53f30
Commit
9fe53f30
authored
10 years ago
by
Sergey Lyubka
Browse files
Options
Downloads
Patches
Plain Diff
Added http client example
parent
c64d30e1
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
examples/http_client/Makefile
+12
-0
12 additions, 0 deletions
examples/http_client/Makefile
examples/http_client/http_client.c
+82
-0
82 additions, 0 deletions
examples/http_client/http_client.c
with
94 additions
and
0 deletions
examples/http_client/Makefile
0 → 100644
+
12
−
0
View file @
9fe53f30
# Copyright (c) 2014 Cesanta Software
# All rights reserved
CFLAGS
=
-W
-Wall
-I
../..
-g
-O0
$(
CFLAGS_EXTRA
)
SOURCES
=
http_client.c ../../mongoose.c
PROG
=
http_client
unix
:
$(SOURCES)
$(
CC
)
-o
$(
PROG
)
$(
SOURCES
)
$(
CFLAGS
)
clean
:
rm
-rf
$(
PROG
)
*
.exe
*
.dSYM
*
.obj
*
.exp .
*
o
*
.lib
This diff is collapsed.
Click to expand it.
examples/http_client/http_client.c
0 → 100644
+
82
−
0
View file @
9fe53f30
// Copyright (c) 2014 Cesanta Software
// All rights reserved
//
// This example demostrates how to connect to the remote Web server,
// download data, process it and send back a reply.
#include
<signal.h>
#include
<stdlib.h>
#include
"mongoose.h"
static
int
s_received_signal
=
0
;
static
struct
mg_server
*
s_server
=
NULL
;
static
const
char
*
s_remote_host
=
"glosbe.com"
;
static
int
s_remote_port
=
80
;
static
void
signal_handler
(
int
sig_num
)
{
signal
(
sig_num
,
signal_handler
);
s_received_signal
=
sig_num
;
}
static
int
ev_handler
(
struct
mg_connection
*
conn
,
enum
mg_event
ev
)
{
struct
mg_connection
*
client
,
*
orig
;
switch
(
ev
)
{
case
MG_AUTH
:
return
MG_TRUE
;
case
MG_CONNECT
:
// Send request to the remote host.
// TODO(lsm): handle connect error here.
mg_printf
(
conn
,
"GET %s HTTP/1.0
\r\n
Host: %s
\r\n\r\n
"
,
"/gapi/translate?from=eng&dest=fra&format=json&phrase=cat"
,
s_remote_host
);
return
MG_TRUE
;
case
MG_REPLY
:
// Send reply to the original connection
orig
=
(
struct
mg_connection
*
)
conn
->
connection_param
;
mg_send_header
(
orig
,
"Content-Type"
,
"text/plain"
);
mg_send_data
(
orig
,
conn
->
content
,
conn
->
content_len
);
mg_send_data
(
orig
,
""
,
0
);
// Last chunk: mark the end of reply
// Disconnect connections
orig
->
connection_param
=
NULL
;
conn
->
connection_param
=
NULL
;
return
MG_TRUE
;
case
MG_REQUEST
:
if
((
client
=
mg_connect
(
s_server
,
s_remote_host
,
s_remote_port
,
0
))
!=
NULL
)
{
// Interconnect requests
client
->
connection_param
=
conn
;
conn
->
connection_param
=
client
;
return
MG_MORE
;
}
else
{
mg_printf_data
(
conn
,
"%s"
,
"cannot send API request"
);
return
MG_TRUE
;
}
default:
return
MG_FALSE
;
}
}
int
main
(
void
)
{
s_server
=
mg_create_server
(
NULL
,
ev_handler
);
mg_set_option
(
s_server
,
"listening_port"
,
"8080"
);
// Setup signal handlers
signal
(
SIGTERM
,
signal_handler
);
signal
(
SIGINT
,
signal_handler
);
printf
(
"Listening on port %s
\n
"
,
mg_get_option
(
s_server
,
"listening_port"
));
while
(
s_received_signal
==
0
)
{
mg_poll_server
(
s_server
,
1000
);
}
mg_destroy_server
(
&
s_server
);
printf
(
"Existing on signal %d
\n
"
,
s_received_signal
);
return
EXIT_SUCCESS
;
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment