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
f977757a
Commit
f977757a
authored
11 years ago
by
Sergey Lyubka
Browse files
Options
Downloads
Patches
Plain Diff
Added mg_set_auth_handler()
parent
2392156f
No related branches found
No related tags found
No related merge requests found
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
examples/auth.c
+8
-21
8 additions, 21 deletions
examples/auth.c
mongoose.c
+12
-0
12 additions, 0 deletions
mongoose.c
mongoose.h
+1
-0
1 addition, 0 deletions
mongoose.h
unit_test.c
+0
-1
0 additions, 1 deletion
unit_test.c
with
21 additions
and
22 deletions
examples/auth.c
+
8
−
21
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
(;;)
{
...
...
This diff is collapsed.
Click to expand it.
mongoose.c
+
12
−
0
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
);
...
...
This diff is collapsed.
Click to expand it.
mongoose.h
+
1
−
0
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
);
...
...
This diff is collapsed.
Click to expand it.
unit_test.c
+
0
−
1
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
;
...
...
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