Răsfoiți Sursa

Hi there,

am I right in the assumption, that the documentation at
http://www.gnu.org/s/libmicrohttpd/tutorial.html#Supporting-basic-authentication
is newer than authorization_example.c in the src/examples/ subdirectory?

If so here is a patch to make authorization_example.c use the functions from
the tutorial instead of raw base64 strings.

Regards

Sven
-
Christian Grothoff 14 ani în urmă
părinte
comite
de35faa11a
3 a modificat fișierele cu 18 adăugiri și 14 ștergeri
  1. 1 0
      AUTHORS
  2. 4 0
      ChangeLog
  3. 13 14
      src/examples/authorization_example.c

+ 1 - 0
AUTHORS

@@ -35,6 +35,7 @@ Zhimin Huang <[email protected]>
 Jan Seeger <[email protected]>
 Will Bryant <[email protected]>
 LRN <[email protected]>
+Sven Geggus <[email protected]>
 
 Documentation contributions also came from:
 Marco Maggi <[email protected]>

+ 4 - 0
ChangeLog

@@ -1,3 +1,7 @@
+Thu Dec  1 15:22:57 CET 2011
+	Updated authorization_example.c to actually demonstrate the current
+	MHD API. -SG
+
 Mon Nov 21 18:51:30 CET 2011
 	Added option to suppress generation of the 'Date:' header to be
 	used on embedded systems without RTC.  Documented the new option

+ 13 - 14
src/examples/authorization_example.c

@@ -44,8 +44,9 @@ ahc_echo (void *cls,
   const char *me = cls;
   struct MHD_Response *response;
   int ret;
-  int code;
-  const char *auth;
+  char *user;
+  char *pass;
+  int fail;
 
   if (0 != strcmp (method, "GET"))
     return MHD_NO;              /* unexpected method */
@@ -56,28 +57,26 @@ ahc_echo (void *cls,
       return MHD_YES;
     }
   *ptr = NULL;                  /* reset when done */
-  auth = MHD_lookup_connection_value (connection,
-                                      MHD_HEADER_KIND,
-                                      MHD_HTTP_HEADER_AUTHORIZATION);
-  if ((auth == NULL) ||
-      (0 != strcmp (auth, "Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==")))
-    {
-      /* require: "Aladdin" with password "open sesame" */
+
+  /* require: "Aladdin" with password "open sesame" */
+  pass = NULL;
+  user = MHD_basic_auth_get_username_password (connection, &pass);
+  fail = ( (user == NULL) || (0 != strcmp (user, "Aladdin")) || (0 != strcmp (pass, "open sesame") ) );
+  if (fail)
+  {
       response = MHD_create_response_from_buffer (strlen (DENIED),
 						  (void *) DENIED, 
 						  MHD_RESPMEM_PERSISTENT);
-      MHD_add_response_header (response, MHD_HTTP_HEADER_WWW_AUTHENTICATE,
-                               "Basic realm=\"TestRealm\"");
-      code = MHD_HTTP_UNAUTHORIZED;
+      ret = MHD_queue_basic_auth_fail_response (connection,"TestRealm",response);
     }
   else
     {
       response = MHD_create_response_from_buffer (strlen (me),
 						  (void *) me, 
 						  MHD_RESPMEM_PERSISTENT);
-      code = MHD_HTTP_OK;
+      ret = MHD_queue_response (connection, MHD_HTTP_OK, response);
     }
-  ret = MHD_queue_response (connection, code, response);
+
   MHD_destroy_response (response);
   return ret;
 }