Bladeren bron

[http] Add support for HTTP Basic authentication

tags/v0.9.7
Michael Brown 15 jaren geleden
bovenliggende
commit
ef70f87997
1 gewijzigde bestanden met toevoegingen van 28 en 0 verwijderingen
  1. 28
    0
      src/net/tcp/http.c

+ 28
- 0
src/net/tcp/http.c Bestand weergeven

@@ -41,6 +41,7 @@
41 41
 #include <gpxe/process.h>
42 42
 #include <gpxe/linebuf.h>
43 43
 #include <gpxe/features.h>
44
+#include <gpxe/base64.h>
44 45
 #include <gpxe/http.h>
45 46
 
46 47
 FEATURE ( FEATURE_PROTOCOL, "HTTP", DHCP_EB_FEATURE_HTTP, 1 );
@@ -142,6 +143,8 @@ static int http_response_to_rc ( unsigned int response ) {
142 143
 		return -ENOENT;
143 144
 	case 403:
144 145
 		return -EPERM;
146
+	case 401:
147
+		return -EACCES;
145 148
 	default:
146 149
 		return -EIO;
147 150
 	}
@@ -387,18 +390,43 @@ static void http_step ( struct process *process ) {
387 390
 	const char *path = http->uri->path;
388 391
 	const char *host = http->uri->host;
389 392
 	const char *query = http->uri->query;
393
+	const char *user = http->uri->user;
394
+	const char *password = http->uri->password;
395
+	size_t user_pw_len = ( ( user && password ) ?
396
+			       ( strlen ( user ) + 1 /* ":" */ +
397
+				 strlen ( password ) ) : 0 );
398
+	size_t user_pw_base64_len = base64_encoded_len ( user_pw_len );
399
+	char user_pw[ user_pw_len + 1 /* NUL */ ];
400
+	char user_pw_base64[ user_pw_base64_len + 1 /* NUL */ ];
390 401
 	int rc;
391 402
 
392 403
 	if ( xfer_window ( &http->socket ) ) {
404
+
405
+		/* We want to execute only once */
393 406
 		process_del ( &http->process );
407
+
408
+		/* Construct authorisation, if applicable */
409
+		if ( user_pw_len ) {
410
+			snprintf ( user_pw, sizeof ( user_pw ), "%s:%s",
411
+				   user, password );
412
+			base64_encode ( user_pw, user_pw_base64 );
413
+		}
414
+
415
+		/* Send GET request */
394 416
 		if ( ( rc = xfer_printf ( &http->socket,
395 417
 					  "GET %s%s%s HTTP/1.0\r\n"
396 418
 					  "User-Agent: gPXE/" VERSION "\r\n"
419
+					  "%s%s%s"
397 420
 					  "Host: %s\r\n"
398 421
 					  "\r\n",
399 422
 					  ( path ? path : "/" ),
400 423
 					  ( query ? "?" : "" ),
401 424
 					  ( query ? query : "" ),
425
+					  ( user_pw_len ?
426
+					    "Authorization: Basic " : "" ),
427
+					  ( user_pw_len ?
428
+					    user_pw_base64 : "" ),
429
+					  ( user_pw_len ? "\r\n" : "" ),
402 430
 					  host ) ) != 0 ) {
403 431
 			http_done ( http, rc );
404 432
 		}

Laden…
Annuleren
Opslaan