Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
M
mongoose
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
esp
mongoose
Commits
460d95af
Commit
460d95af
authored
Sep 30, 2016
by
Deomid Ryabkov
Committed by
Cesanta Bot
Sep 30, 2016
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add mg_http_send_redirect and make examples use it
PUBLISHED_FROM=f45e06aeba1c00212078d539b6f74cb8bd054326
parent
8f522048
Changes
12
Hide whitespace changes
Inline
Side-by-side
Showing
12 changed files
with
119 additions
and
41 deletions
+119
-41
intro.md
docs/c-api/http_server.h/intro.md
+1
-0
mg_http_send_redirect.md
docs/c-api/http_server.h/mg_http_send_redirect.md
+24
-0
mg_send_response_line.md
docs/c-api/http_server.h/mg_send_response_line.md
+1
-1
intro.md
docs/c-api/util.h/intro.md
+1
-0
mg_asprintf.md
docs/c-api/util.h/mg_asprintf.md
+22
-0
mg_avprintf.md
docs/c-api/util.h/mg_avprintf.md
+1
-13
server.c
examples/connected_device_2/server.c
+1
-1
server.c
examples/connected_device_3/server.c
+1
-1
server.c
examples/connected_device_4/server.c
+1
-1
cookie_auth.c
examples/cookie_auth/cookie_auth.c
+10
-22
mongoose.c
mongoose.c
+31
-0
mongoose.h
mongoose.h
+25
-2
No files found.
docs/c-api/http_server.h/intro.md
View file @
460d95af
...
...
@@ -16,6 +16,7 @@ items:
-
{
name
:
mg_send_http_chunk.md
}
-
{
name
:
mg_printf_http_chunk.md
}
-
{
name
:
mg_send_response_line.md
}
-
{
name
:
mg_http_send_redirect.md
}
-
{
name
:
mg_send_head.md
}
-
{
name
:
mg_printf_html_escape.md
}
-
{
name
:
mg_fu_fname_fn.md
}
...
...
docs/c-api/http_server.h/mg_http_send_redirect.md
0 → 100644
View file @
460d95af
---
title
:
"
mg_http_send_redirect()"
decl_name
:
"
mg_http_send_redirect"
symbol_kind
:
"
func"
signature
:
|
void mg_http_send_redirect(struct mg_connection *nc, int status_code,
const struct mg_str location,
const struct mg_str extra_headers);
---
Sends a redirect response.
`status_code`
should be either 301 or 302 and
`location`
point to the
new location.
If
`extra_headers`
is not empty, then
`extra_headers`
are also sent
after the reponse line.
`extra_headers`
must NOT end end with new line.
Example:
mg_send_response_line(nc, 200, "Access-Control-Allow-Origin: *");
Will result in:
HTTP/1.1 200 OK\r\n
Access-Control-Allow-Origin: *\r\n
docs/c-api/http_server.h/mg_send_response_line.md
View file @
460d95af
...
...
@@ -3,7 +3,7 @@ title: "mg_send_response_line()"
decl_name
:
"
mg_send_response_line"
symbol_kind
:
"
func"
signature
:
|
void mg_send_response_line(struct mg_connection *c, int status_code,
void mg_send_response_line(struct mg_connection *
n
c, int status_code,
const char *extra_headers);
---
...
...
docs/c-api/util.h/intro.md
View file @
460d95af
...
...
@@ -17,6 +17,7 @@ items:
-
{
name
:
mg_sock_addr_to_str.md
}
-
{
name
:
mg_hexdump.md
}
-
{
name
:
mg_hexdump_connection.md
}
-
{
name
:
mg_asprintf.md
}
-
{
name
:
mg_avprintf.md
}
-
{
name
:
mg_is_big_endian.md
}
-
{
name
:
mg_next_comma_list_entry.md
}
...
...
docs/c-api/util.h/mg_asprintf.md
0 → 100644
View file @
460d95af
---
title
:
"
mg_asprintf()"
decl_name
:
"
mg_asprintf"
symbol_kind
:
"
func"
signature
:
|
int mg_asprintf(char **buf, size_t size, const char *fmt, ...);
---
Prints message to the buffer. If the buffer is large enough to hold the
message, it returns buffer. If buffer is to small, it allocates a large
enough buffer on heap and returns allocated buffer.
This is a supposed use case:
char buf
[
5
]
,
*
p = buf;
mg_avprintf(&p, sizeof(buf), "%s", "hi there");
use_p_somehow(p);
if (p != buf) {
free(p);
}
The purpose of this is to avoid malloc-ing if generated strings are small.
docs/c-api/util.h/mg_avprintf.md
View file @
460d95af
...
...
@@ -6,17 +6,5 @@ signature: |
int mg_avprintf(char **buf, size_t size, const char *fmt, va_list ap);
---
Prints message to the buffer. If the buffer is large enough to hold the
message, it returns buffer. If buffer is to small, it allocates a large
enough buffer on heap and returns allocated buffer.
This is a supposed use case:
char buf
[
5
]
,
*
p = buf;
p = mg_avprintf(&p, sizeof(buf), "%s", "hi there");
use_p_somehow(p);
if (p != buf) {
free(p);
}
The purpose of this is to avoid malloc-ing if generated strings are small.
Same as mg_asprintf, but takes varargs list.
examples/connected_device_2/server.c
View file @
460d95af
...
...
@@ -20,7 +20,7 @@ static void handle_save(struct mg_connection *nc, struct http_message *hm) {
sizeof
(
s_settings
.
setting2
));
// Send response
mg_
printf
(
nc
,
"%s"
,
"HTTP/1.1 302 OK
\r\n
Location: /
\r\n\r\n
"
);
mg_
http_send_redirect
(
nc
,
302
,
mg_mk_str
(
"/"
),
mg_mk_str
(
NULL
)
);
}
static
void
handle_ssi_call
(
struct
mg_connection
*
nc
,
const
char
*
param
)
{
...
...
examples/connected_device_3/server.c
View file @
460d95af
...
...
@@ -20,7 +20,7 @@ static void handle_save(struct mg_connection *nc, struct http_message *hm) {
sizeof
(
s_settings
.
setting2
));
// Send response
mg_
printf
(
nc
,
"%s"
,
"HTTP/1.1 302 OK
\r\n
Location: /
\r\n\r\n
"
);
mg_
http_send_redirect
(
nc
,
302
,
mg_mk_str
(
"/"
),
mg_mk_str
(
NULL
)
);
}
static
void
handle_get_cpu_usage
(
struct
mg_connection
*
nc
)
{
...
...
examples/connected_device_4/server.c
View file @
460d95af
...
...
@@ -20,7 +20,7 @@ static void handle_save(struct mg_connection *nc, struct http_message *hm) {
sizeof
(
s_settings
.
setting2
));
// Send response
mg_
printf
(
nc
,
"%s"
,
"HTTP/1.1 302 OK
\r\n
Location: /
\r\n\r\n
"
);
mg_
http_send_redirect
(
nc
,
302
,
mg_mk_str
(
"/"
),
mg_mk_str
(
NULL
)
);
}
static
void
handle_get_cpu_usage
(
struct
mg_connection
*
nc
)
{
...
...
examples/cookie_auth/cookie_auth.c
View file @
460d95af
...
...
@@ -120,12 +120,6 @@ static struct session *create_session(const char *user,
return
s
;
}
static
void
set_session_cookie
(
struct
mg_connection
*
nc
,
const
struct
session
*
s
)
{
mg_printf
(
nc
,
"Set-Cookie: %s=%"
INT64_X_FMT
"; path=/
\r\n
"
,
SESSION_COOKIE_NAME
,
s
->
id
);
}
/*
* If requested via GET, serves the login page.
* If requested via POST (form submission), checks password and logs user in.
...
...
@@ -143,10 +137,11 @@ static void login_handler(struct mg_connection *nc, int ev, void *p) {
if
(
ul
>
0
&&
pl
>
0
)
{
if
(
check_pass
(
user
,
pass
))
{
struct
session
*
s
=
create_session
(
user
,
hm
);
mg_printf
(
nc
,
"HTTP/1.0 302 Found
\r\n
"
);
set_session_cookie
(
nc
,
s
);
mg_printf
(
nc
,
"Location: /
\r\n
"
);
mg_printf
(
nc
,
"
\r\n
Hello, %s!
\r\n
"
,
s
->
user
);
char
shead
[
100
];
snprintf
(
shead
,
sizeof
(
shead
),
"Set-Cookie: %s=%"
INT64_X_FMT
"; path=/"
,
SESSION_COOKIE_NAME
,
s
->
id
);
mg_http_send_redirect
(
nc
,
302
,
mg_mk_str
(
"/"
),
mg_mk_str
(
shead
));
fprintf
(
stderr
,
"%s logged in, sid %"
INT64_X_FMT
"
\n
"
,
s
->
user
,
s
->
id
);
}
else
{
mg_printf
(
nc
,
"HTTP/1.0 403 Unauthorized
\r\n\r\n
Wrong password.
\r\n
"
);
...
...
@@ -165,13 +160,9 @@ static void login_handler(struct mg_connection *nc, int ev, void *p) {
*/
static
void
logout_handler
(
struct
mg_connection
*
nc
,
int
ev
,
void
*
p
)
{
struct
http_message
*
hm
=
(
struct
http_message
*
)
p
;
mg_printf
(
nc
,
"HTTP/1.0 302 Found
\r\n
"
"Set-Cookie: %s=
\r\n
"
"Location: /
\r\n
"
"
\r\n
"
"Logged out"
,
SESSION_COOKIE_NAME
);
char
shead
[
100
];
snprintf
(
shead
,
sizeof
(
shead
),
"Set-Cookie: %s="
,
SESSION_COOKIE_NAME
);
mg_http_send_redirect
(
nc
,
302
,
mg_mk_str
(
"/"
),
mg_mk_str
(
shead
));
struct
session
*
s
=
get_session
(
hm
);
if
(
s
!=
NULL
)
{
fprintf
(
stderr
,
"%s logged out, session %"
INT64_X_FMT
" destroyed
\n
"
,
...
...
@@ -203,11 +194,8 @@ static void ev_handler(struct mg_connection *nc, int ev, void *p) {
struct
session
*
s
=
get_session
(
hm
);
/* Ask the user to log in if they did not present a valid cookie. */
if
(
s
==
NULL
)
{
mg_printf
(
nc
,
"HTTP/1.0 302 Found
\r\n
"
"Location: /login.html
\r\n
"
"
\r\n
"
"Please log in"
);
mg_http_send_redirect
(
nc
,
302
,
mg_mk_str
(
"/login.html"
),
mg_mk_str
(
NULL
));
nc
->
flags
|=
MG_F_SEND_AND_CLOSE
;
break
;
}
...
...
mongoose.c
View file @
460d95af
...
...
@@ -5336,6 +5336,28 @@ void mg_send_response_line(struct mg_connection *nc, int status_code,
mg_send_response_line_s
(
nc
,
status_code
,
mg_mk_str
(
extra_headers
));
}
void
mg_http_send_redirect
(
struct
mg_connection
*
nc
,
int
status_code
,
const
struct
mg_str
location
,
const
struct
mg_str
extra_headers
)
{
char
bbody
[
100
],
*
pbody
=
bbody
;
int
bl
=
mg_asprintf
(
&
pbody
,
sizeof
(
bbody
),
"<p>Moved <a href='%.*s'>here</a>.
\r\n
"
,
(
int
)
location
.
len
,
location
.
p
);
char
bhead
[
150
],
*
phead
=
bhead
;
mg_asprintf
(
&
phead
,
sizeof
(
bhead
),
"Location: %.*s
\r\n
"
"Content-Type: text/html
\r\n
"
"Content-Length: %d
\r\n
"
"Cache-Control: no-cache
\r\n
"
"%.*s%s"
,
(
int
)
location
.
len
,
location
.
p
,
bl
,
(
int
)
extra_headers
.
len
,
extra_headers
.
p
,
(
extra_headers
.
len
>
0
?
"
\r\n
"
:
""
));
mg_send_response_line
(
nc
,
status_code
,
phead
);
if
(
phead
!=
bhead
)
MG_FREE
(
phead
);
mg_send
(
nc
,
pbody
,
bl
);
if
(
pbody
!=
bbody
)
MG_FREE
(
pbody
);
}
void
mg_send_head
(
struct
mg_connection
*
c
,
int
status_code
,
int64_t
content_length
,
const
char
*
extra_headers
)
{
mg_send_response_line
(
c
,
status_code
,
extra_headers
);
...
...
@@ -7754,6 +7776,15 @@ int mg_avprintf(char **buf, size_t size, const char *fmt, va_list ap) {
return
len
;
}
int
mg_asprintf
(
char
**
buf
,
size_t
size
,
const
char
*
fmt
,
...)
{
int
ret
;
va_list
ap
;
va_start
(
ap
,
fmt
);
ret
=
mg_avprintf
(
buf
,
size
,
fmt
,
ap
);
va_end
(
ap
);
return
ret
;
}
#if !defined(MG_DISABLE_HEXDUMP)
void
mg_hexdump_connection
(
struct
mg_connection
*
nc
,
const
char
*
path
,
const
void
*
buf
,
int
num_bytes
,
int
ev
)
{
...
...
mongoose.h
View file @
460d95af
...
...
@@ -2009,6 +2009,7 @@ int mg_hexdump(const void *buf, int len, char *dst, int dst_len);
*/
void
mg_hexdump_connection
(
struct
mg_connection
*
nc
,
const
char
*
path
,
const
void
*
buf
,
int
num_bytes
,
int
ev
);
/*
* Prints message to the buffer. If the buffer is large enough to hold the
* message, it returns buffer. If buffer is to small, it allocates a large
...
...
@@ -2016,7 +2017,7 @@ void mg_hexdump_connection(struct mg_connection *nc, const char *path,
* This is a supposed use case:
*
* char buf[5], *p = buf;
*
p =
mg_avprintf(&p, sizeof(buf), "%s", "hi there");
* mg_avprintf(&p, sizeof(buf), "%s", "hi there");
* use_p_somehow(p);
* if (p != buf) {
* free(p);
...
...
@@ -2024,6 +2025,9 @@ void mg_hexdump_connection(struct mg_connection *nc, const char *path,
*
* The purpose of this is to avoid malloc-ing if generated strings are small.
*/
int
mg_asprintf
(
char
**
buf
,
size_t
size
,
const
char
*
fmt
,
...);
/* Same as mg_asprintf, but takes varargs list. */
int
mg_avprintf
(
char
**
buf
,
size_t
size
,
const
char
*
fmt
,
va_list
ap
);
/*
...
...
@@ -2800,9 +2804,28 @@ void mg_printf_http_chunk(struct mg_connection *nc, const char *fmt, ...);
* HTTP/1.1 200 OK\r\n
* Access-Control-Allow-Origin: *\r\n
*/
void
mg_send_response_line
(
struct
mg_connection
*
c
,
int
status_code
,
void
mg_send_response_line
(
struct
mg_connection
*
n
c
,
int
status_code
,
const
char
*
extra_headers
);
/*
* Sends a redirect response.
* `status_code` should be either 301 or 302 and `location` point to the
* new location.
* If `extra_headers` is not empty, then `extra_headers` are also sent
* after the reponse line. `extra_headers` must NOT end end with new line.
* Example:
*
* mg_send_response_line(nc, 200, "Access-Control-Allow-Origin: *");
*
* Will result in:
*
* HTTP/1.1 200 OK\r\n
* Access-Control-Allow-Origin: *\r\n
*/
void
mg_http_send_redirect
(
struct
mg_connection
*
nc
,
int
status_code
,
const
struct
mg_str
location
,
const
struct
mg_str
extra_headers
);
/*
* Sends the response line and headers.
* This function sends the response line with the `status_code`, and
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment