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
f977757a
Commit
f977757a
authored
Jan 24, 2014
by
Sergey Lyubka
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added mg_set_auth_handler()
parent
2392156f
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
21 additions
and
22 deletions
+21
-22
auth.c
examples/auth.c
+8
-21
mongoose.c
mongoose.c
+12
-0
mongoose.h
mongoose.h
+1
-0
unit_test.c
unit_test.c
+0
-1
No files found.
examples/auth.c
View file @
f977757a
...
...
@@ -2,38 +2,25 @@
#include <string.h>
#include "mongoose.h"
static
int
index_html
(
struct
mg_connection
*
conn
)
{
mg_send_header
(
conn
,
"Content-Type"
,
"text/html"
);
mg_printf_data
(
conn
,
"%s"
,
"This link is password-protected: <a href=/secret>link</a>"
);
return
1
;
}
static
int
secret_html
(
struct
mg_connection
*
conn
)
{
static
const
char
*
passwords_file
=
"my_passwords.txt"
;
FILE
*
fp
=
fopen
(
passwords_file
,
"r"
);
static
int
auth_handler
(
struct
mg_connection
*
conn
)
{
int
result
=
0
;
// Not authorized
FILE
*
fp
;
// To populate passwords file, do
// mongoose -A my_passwords.txt mydomain.com admin admin
if
(
mg_authorize_digest
(
conn
,
fp
))
{
mg_printf_data
(
conn
,
"%s"
,
"Hi, here is a secret message!"
);
}
else
{
mg_send_digest_auth_request
(
conn
);
}
if
(
fp
!=
NULL
)
{
if
((
fp
=
fopen
(
"my_passwords.txt"
,
"r"
))
!=
NULL
)
{
result
=
mg_authorize_digest
(
conn
,
fp
);
fclose
(
fp
);
}
return
1
;
return
result
;
}
int
main
(
void
)
{
struct
mg_server
*
server
=
mg_create_server
(
NULL
);
mg_set_option
(
server
,
"listening_port"
,
"8080"
);
mg_
add_uri_handler
(
server
,
"/"
,
index_html
);
mg_
add_uri_handler
(
server
,
"/secret"
,
secret_html
);
mg_
set_option
(
server
,
"document_root"
,
"."
);
mg_
set_auth_handler
(
server
,
auth_handler
);
printf
(
"Starting on port %s
\n
"
,
mg_get_option
(
server
,
"listening_port"
));
for
(;;)
{
...
...
mongoose.c
View file @
f977757a
...
...
@@ -273,6 +273,7 @@ struct mg_server {
struct
ll
active_connections
;
struct
ll
uri_handlers
;
mg_handler_t
error_handler
;
mg_handler_t
auth_handler
;
char
*
config_options
[
NUM_OPTIONS
];
void
*
server_data
;
#ifdef MONGOOSE_USE_SSL
...
...
@@ -3358,6 +3359,13 @@ static void open_local_endpoint(struct connection *conn) {
const
char
*
dir_lst
=
conn
->
server
->
config_options
[
ENABLE_DIRECTORY_LISTING
];
#endif
// Call auth handler
if
(
conn
->
server
->
auth_handler
!=
NULL
&&
conn
->
server
->
auth_handler
(
&
conn
->
mg_conn
)
==
0
)
{
mg_send_digest_auth_request
(
&
conn
->
mg_conn
);
return
;
}
// Call URI handler if one is registered for this URI
conn
->
endpoint
.
uh
=
find_uri_handler
(
conn
->
server
,
conn
->
mg_conn
.
uri
);
if
(
conn
->
endpoint
.
uh
!=
NULL
)
{
...
...
@@ -4117,6 +4125,10 @@ void mg_set_http_error_handler(struct mg_server *server, mg_handler_t handler) {
server
->
error_handler
=
handler
;
}
void
mg_set_auth_handler
(
struct
mg_server
*
server
,
mg_handler_t
handler
)
{
server
->
auth_handler
=
handler
;
}
void
mg_set_listening_socket
(
struct
mg_server
*
server
,
int
sock
)
{
if
(
server
->
listening_sock
!=
INVALID_SOCKET
)
{
closesocket
(
server
->
listening_sock
);
...
...
mongoose.h
View file @
f977757a
...
...
@@ -65,6 +65,7 @@ const char *mg_set_option(struct mg_server *, const char *opt, const char *val);
unsigned
int
mg_poll_server
(
struct
mg_server
*
,
int
milliseconds
);
void
mg_add_uri_handler
(
struct
mg_server
*
,
const
char
*
uri
,
mg_handler_t
);
void
mg_set_http_error_handler
(
struct
mg_server
*
,
mg_handler_t
);
void
mg_set_auth_handler
(
struct
mg_server
*
,
mg_handler_t
);
const
char
**
mg_get_valid_option_names
(
void
);
const
char
*
mg_get_option
(
const
struct
mg_server
*
server
,
const
char
*
name
);
void
mg_set_listening_socket
(
struct
mg_server
*
,
int
sock
);
...
...
unit_test.c
View file @
f977757a
...
...
@@ -475,7 +475,6 @@ static int cb4(struct mg_connection *conn) {
}
static
int
cb3
(
struct
mg_connection
*
conn
)
{
printf
(
"cb3: %d
\n
"
,
conn
->
status_code
);
fflush
(
stdout
);
sprintf
((
char
*
)
conn
->
connection_param
,
"%d"
,
conn
->
status_code
);
return
1
;
...
...
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