diff options
-rw-r--r-- | src/examples/digest_auth_example.c | 76 |
1 files changed, 40 insertions, 36 deletions
diff --git a/src/examples/digest_auth_example.c b/src/examples/digest_auth_example.c index 98b50382..6221517e 100644 --- a/src/examples/digest_auth_example.c +++ b/src/examples/digest_auth_example.c | |||
@@ -45,39 +45,24 @@ ahc_echo (void *cls, | |||
45 | int ret; | 45 | int ret; |
46 | 46 | ||
47 | username = MHD_digest_auth_get_username(connection); | 47 | username = MHD_digest_auth_get_username(connection); |
48 | 48 | if (username == NULL) | |
49 | if (username == NULL) { | 49 | return MHD_queue_auth_fail_response(connection, realm, |
50 | ret = MHD_queue_auth_fail_response(connection, realm, | 50 | OPAQUE, |
51 | OPAQUE, | 51 | MHD_NO); |
52 | MHD_NO); | ||
53 | |||
54 | return ret; | ||
55 | } | ||
56 | |||
57 | ret = MHD_digest_auth_check(connection, realm, | 52 | ret = MHD_digest_auth_check(connection, realm, |
58 | username, password, 300); | 53 | username, |
59 | 54 | password, | |
55 | 300); | ||
60 | free(username); | 56 | free(username); |
61 | 57 | if (ret == MHD_INVALID_NONCE) | |
62 | if (ret == MHD_INVALID_NONCE) { | 58 | return MHD_queue_auth_fail_response(connection, realm, |
63 | ret = MHD_queue_auth_fail_response(connection, realm, | 59 | OPAQUE, MHD_YES); |
64 | OPAQUE, MHD_YES); | 60 | if (ret == MHD_NO) |
65 | 61 | return MHD_queue_auth_fail_response(connection, realm, | |
66 | return ret; | 62 | OPAQUE, MHD_NO); |
67 | } | ||
68 | |||
69 | if (ret == MHD_NO) { | ||
70 | ret = MHD_queue_auth_fail_response(connection, realm, | ||
71 | OPAQUE, MHD_NO); | ||
72 | |||
73 | return ret; | ||
74 | } | ||
75 | |||
76 | response = MHD_create_response_from_data(strlen(PAGE), PAGE, | 63 | response = MHD_create_response_from_data(strlen(PAGE), PAGE, |
77 | MHD_NO, MHD_NO); | 64 | MHD_NO, MHD_NO); |
78 | 65 | ret = MHD_queue_response(connection, MHD_HTTP_OK, response); | |
79 | ret = MHD_queue_response(connection, MHD_HTTP_OK, response); | ||
80 | |||
81 | MHD_destroy_response(response); | 66 | MHD_destroy_response(response); |
82 | return ret; | 67 | return ret; |
83 | } | 68 | } |
@@ -88,6 +73,7 @@ main (int argc, char *const *argv) | |||
88 | int fd; | 73 | int fd; |
89 | char rnd[9]; | 74 | char rnd[9]; |
90 | size_t len; | 75 | size_t len; |
76 | size_t off; | ||
91 | struct MHD_Daemon *d; | 77 | struct MHD_Daemon *d; |
92 | 78 | ||
93 | if (argc != 2) | 79 | if (argc != 2) |
@@ -95,21 +81,39 @@ main (int argc, char *const *argv) | |||
95 | printf ("%s PORT\n", argv[0]); | 81 | printf ("%s PORT\n", argv[0]); |
96 | return 1; | 82 | return 1; |
97 | } | 83 | } |
98 | |||
99 | fd = open("/dev/urandom", O_RDONLY); | 84 | fd = open("/dev/urandom", O_RDONLY); |
100 | len = read(fd, rnd, 8); | 85 | if (-1 == fd) |
86 | { | ||
87 | fprintf (stderr, "Failed to open `%s': %s\n", | ||
88 | "/dev/urandom", | ||
89 | strerror (errno)); | ||
90 | return 1; | ||
91 | } | ||
92 | off = 0; | ||
93 | while (off < 8) | ||
94 | { | ||
95 | len = read(fd, rnd, 8); | ||
96 | if (len == -1) | ||
97 | { | ||
98 | fprintf (stderr, "Failed to read `%s': %s\n", | ||
99 | "/dev/urandom", | ||
100 | strerror (errno)); | ||
101 | return 1; | ||
102 | } | ||
103 | off += len; | ||
104 | } | ||
101 | close(fd); | 105 | close(fd); |
102 | |||
103 | rnd[8] = '\0'; | 106 | rnd[8] = '\0'; |
104 | |||
105 | d = MHD_start_daemon (MHD_USE_THREAD_PER_CONNECTION | MHD_USE_DEBUG, | 107 | d = MHD_start_daemon (MHD_USE_THREAD_PER_CONNECTION | MHD_USE_DEBUG, |
106 | atoi (argv[1]), | 108 | atoi (argv[1]), |
107 | NULL, NULL, &ahc_echo, PAGE, | 109 | NULL, NULL, &ahc_echo, PAGE, |
108 | MHD_OPTION_DIGEST_AUTH_RANDOM, rnd, | 110 | MHD_OPTION_DIGEST_AUTH_RANDOM, rnd, |
109 | MHD_OPTION_END); | 111 | MHD_OPTION_END); |
110 | if (d == NULL) | 112 | if (d == NULL) |
111 | return 1; | 113 | return 1; |
112 | (void) getc (stdin); | 114 | (void) getc (stdin); |
113 | MHD_stop_daemon (d); | 115 | MHD_stop_daemon (d); |
114 | return 0; | 116 | return 0; |
115 | } | 117 | } |
118 | |||
119 | /* end of digest_auth_example.c */ | ||