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
cc636197
Commit
cc636197
authored
Jan 13, 2014
by
Sergey Lyubka
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added auth example
parent
6b4f7e78
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
53 additions
and
3 deletions
+53
-3
Embed.md
docs/Embed.md
+2
-1
Makefile
examples/Makefile
+2
-0
auth.c
examples/auth.c
+45
-0
mongoose.c
mongoose.c
+4
-2
No files found.
docs/Embed.md
View file @
cc636197
...
...
@@ -90,7 +90,8 @@ a couple of kilobytes to the executable size, and also has some runtime penalty.
Mongoose source code contains a well-commented example code, listed below:
*
[
hello.c
](
https://github.com/cesanta/mongoose/blob/master/examples/hello.c
)
is
a minimalistic hello world example
a minimalistic hello world example
*
[
post.c
](
https://github.com/cesanta/mongoose/blob/master/examples/post.c
)
shows how to handle form input
*
[
upload.c
](
https://github.com/cesanta/mongoose/blob/master/examples/post.c
)
shows how to upload files
examples/Makefile
View file @
cc636197
...
...
@@ -24,6 +24,7 @@ all:
$(CC)
post.c ../mongoose.c
-o
post
$(CFLAGS)
$(CC)
multi_threaded.c ../mongoose.c
-o
multi_threaded
$(CFLAGS)
$(CC)
upload.c ../mongoose.c
-o
upload
$(CFLAGS)
$(CC)
auth.c ../mongoose.c
-o
auth
$(CFLAGS)
# $(CC) -DUSE_WEBSOCKET websocket.c ../mongoose.c -o $@ $(CFLAGS)
# $(CC) chat.c ../mongoose.c -o chat $(CFLAGS)
...
...
@@ -41,6 +42,7 @@ windows:
$(CL)
post.c ../mongoose.c
$(CLFLAGS)
$(LFLAGS)
$(CL)
multi_threaded.c ../mongoose.c
$(CLFLAGS)
$(LFLAGS)
$(CL)
upload.c ../mongoose.c
$(CLFLAGS)
$(LFLAGS)
$(CL)
auth.c ../mongoose.c
$(CLFLAGS)
$(LFLAGS)
# $(CL) /DUSE_WEBSOCKET websocket.c ../mongoose.c $(CLFLAGS) $(LFLAGS)
#$(CL) lua_dll.c $(CLFLAGS) $(DLL_FLAGS) /DLL $(LFLAGS) /SUBSYSTEM:WINDOWS /ENTRY:luaopen_lua_dll /EXPORT:luaopen_lua_dll /out:lua_dll.dll
...
...
examples/auth.c
0 → 100644
View file @
cc636197
#include <stdio.h>
#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"
);
// 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
)
{
fclose
(
fp
);
}
return
1
;
}
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
);
printf
(
"Starting on port %s
\n
"
,
mg_get_option
(
server
,
"listening_port"
));
for
(;;)
{
mg_poll_server
(
server
,
1000
);
}
mg_destroy_server
(
&
server
);
return
0
;
}
mongoose.c
View file @
cc636197
...
...
@@ -2921,11 +2921,13 @@ static int check_password(const char *method, const char *ha1, const char *uri,
// Authorize against the opened passwords file. Return 1 if authorized.
int
mg_authorize_digest
(
struct
mg_connection
*
c
,
FILE
*
fp
)
{
struct
connection
*
conn
=
(
struct
connection
*
)
c
;
const
char
*
hdr
=
mg_get_header
(
c
,
"Authorization"
)
;
const
char
*
hdr
;
char
line
[
256
],
f_user
[
256
],
ha1
[
256
],
f_domain
[
256
],
user
[
100
],
nonce
[
100
],
uri
[
MAX_REQUEST_SIZE
],
cnonce
[
100
],
resp
[
100
],
qop
[
100
],
nc
[
100
];
if
(
hdr
==
NULL
||
mg_strncasecmp
(
hdr
,
"Digest "
,
7
)
!=
0
)
return
0
;
if
(
c
==
NULL
||
fp
==
NULL
)
return
0
;
if
((
hdr
=
mg_get_header
(
c
,
"Authorization"
))
==
NULL
||
mg_strncasecmp
(
hdr
,
"Digest "
,
7
)
!=
0
)
return
0
;
if
(
!
mg_parse_header
(
hdr
,
"username"
,
user
,
sizeof
(
user
)))
return
0
;
if
(
!
mg_parse_header
(
hdr
,
"cnonce"
,
cnonce
,
sizeof
(
cnonce
)))
return
0
;
if
(
!
mg_parse_header
(
hdr
,
"response"
,
resp
,
sizeof
(
resp
)))
return
0
;
...
...
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