Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in / Register
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
Packages
Packages
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
submodule
mongoose
Commits
7c4cda62
Commit
7c4cda62
authored
Nov 26, 2015
by
Sergey Lyubka
Committed by
Sergey Lyubka
Nov 30, 2015
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Implement port based redirect
PUBLISHED_FROM=798a8bc38cb385027d3caae9b954002c1a10d39c
parent
d8f8c1cb
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
46 additions
and
1 deletion
+46
-1
restful_server.c
examples/restful_server/restful_server.c
+1
-1
mongoose.c
mongoose.c
+38
-0
mongoose.h
mongoose.h
+7
-0
No files found.
examples/restful_server/restful_server.c
View file @
7c4cda62
...
...
@@ -74,7 +74,7 @@ int main(int argc, char *argv[]) {
#endif
}
else
if
(
strcmp
(
argv
[
i
],
"-P"
)
==
0
&&
i
+
1
<
argc
)
{
s_http_server_opts
.
global_auth_file
=
argv
[
++
i
];
}
else
if
(
strcmp
(
argv
[
i
],
"-
p
"
)
==
0
&&
i
+
1
<
argc
)
{
}
else
if
(
strcmp
(
argv
[
i
],
"-
A
"
)
==
0
&&
i
+
1
<
argc
)
{
s_http_server_opts
.
per_directory_auth_file
=
argv
[
++
i
];
}
else
if
(
strcmp
(
argv
[
i
],
"-r"
)
==
0
&&
i
+
1
<
argc
)
{
s_http_server_opts
.
url_rewrites
=
argv
[
++
i
];
...
...
mongoose.c
View file @
7c4cda62
...
...
@@ -4488,6 +4488,12 @@ void mg_send_response_line(struct mg_connection *nc, int status_code,
case
206
:
status_message
=
"Partial Content"
;
break
;
case
301
:
status_message
=
"Moved"
;
break
;
case
302
:
status_message
=
"Found"
;
break
;
case
416
:
status_message
=
"Requested range not satisfiable"
;
break
;
...
...
@@ -5502,6 +5508,34 @@ MG_INTERNAL int find_index_file(char *path, size_t path_len, const char *list,
return
found
;
}
static
int
send_port_based_redirect
(
struct
mg_connection
*
c
,
struct
http_message
*
hm
,
const
struct
mg_serve_http_opts
*
opts
)
{
const
char
*
rewrites
=
opts
->
url_rewrites
;
struct
mg_str
a
,
b
;
char
local_port
[
20
]
=
{
'%'
};
#ifndef MG_ESP8266
mg_sock_to_str
(
c
->
sock
,
local_port
+
1
,
sizeof
(
local_port
)
-
1
,
MG_SOCK_STRINGIFY_PORT
);
#else
/* TODO(lsm): remove when mg_sock_to_str() is implemented in LWIP codepath */
snprintf
(
local_port
,
sizeof
(
local_port
),
"%s"
,
"%0"
);
#endif
while
((
rewrites
=
mg_next_comma_list_entry
(
rewrites
,
&
a
,
&
b
))
!=
NULL
)
{
if
(
mg_vcmp
(
&
a
,
local_port
)
==
0
)
{
mg_send_response_line
(
c
,
301
,
NULL
);
mg_printf
(
c
,
"Content-Length: 0
\r\n
Location: %.*s%.*s
\r\n\r\n
"
,
(
int
)
b
.
len
,
b
.
p
,
(
int
)
(
hm
->
proto
.
p
-
hm
->
uri
.
p
-
1
),
hm
->
uri
.
p
);
return
1
;
}
}
return
0
;
}
static
void
uri_to_path
(
struct
http_message
*
hm
,
char
*
buf
,
size_t
buf_len
,
const
struct
mg_serve_http_opts
*
opts
)
{
char
uri
[
MG_MAX_PATH
];
...
...
@@ -6132,6 +6166,10 @@ void mg_serve_http(struct mg_connection *nc, struct http_message *hm,
char
path
[
MG_MAX_PATH
];
struct
mg_str
*
hdr
;
if
(
send_port_based_redirect
(
nc
,
hm
,
&
opts
))
{
return
;
}
if
(
opts
.
document_root
==
NULL
)
{
opts
.
document_root
=
"."
;
}
...
...
mongoose.h
View file @
7c4cda62
...
...
@@ -1772,6 +1772,13 @@ struct mg_serve_http_opts {
* If uri_pattern starts with `@` symbol, then Mongoose compares it with the
* HOST header of the request. If they are equal, Mongoose sets document root
* to `file_or_directory_path`, implementing virtual hosts support.
* Example: `@foo.com=/document/root/for/foo.com`
*
* If `uri_pattern` starts with `%` symbol, then Mongoose compares it with
* the listening port. If they match, then Mongoose issues a 301 redirect.
* For example, to redirect all HTTP requests to the
* HTTPS port, do `%80=https://my.site.com`. Note that the request URI is
* automatically appended to the redirect location.
*/
const
char
*
url_rewrites
;
...
...
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