Browse Source

[http] Add support for HTTP Basic authentication

tags/v0.9.7
Michael Brown 15 years ago
parent
commit
ef70f87997
1 changed files with 28 additions and 0 deletions
  1. 28
    0
      src/net/tcp/http.c

+ 28
- 0
src/net/tcp/http.c View File

41
 #include <gpxe/process.h>
41
 #include <gpxe/process.h>
42
 #include <gpxe/linebuf.h>
42
 #include <gpxe/linebuf.h>
43
 #include <gpxe/features.h>
43
 #include <gpxe/features.h>
44
+#include <gpxe/base64.h>
44
 #include <gpxe/http.h>
45
 #include <gpxe/http.h>
45
 
46
 
46
 FEATURE ( FEATURE_PROTOCOL, "HTTP", DHCP_EB_FEATURE_HTTP, 1 );
47
 FEATURE ( FEATURE_PROTOCOL, "HTTP", DHCP_EB_FEATURE_HTTP, 1 );
142
 		return -ENOENT;
143
 		return -ENOENT;
143
 	case 403:
144
 	case 403:
144
 		return -EPERM;
145
 		return -EPERM;
146
+	case 401:
147
+		return -EACCES;
145
 	default:
148
 	default:
146
 		return -EIO;
149
 		return -EIO;
147
 	}
150
 	}
387
 	const char *path = http->uri->path;
390
 	const char *path = http->uri->path;
388
 	const char *host = http->uri->host;
391
 	const char *host = http->uri->host;
389
 	const char *query = http->uri->query;
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
 	int rc;
401
 	int rc;
391
 
402
 
392
 	if ( xfer_window ( &http->socket ) ) {
403
 	if ( xfer_window ( &http->socket ) ) {
404
+
405
+		/* We want to execute only once */
393
 		process_del ( &http->process );
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
 		if ( ( rc = xfer_printf ( &http->socket,
416
 		if ( ( rc = xfer_printf ( &http->socket,
395
 					  "GET %s%s%s HTTP/1.0\r\n"
417
 					  "GET %s%s%s HTTP/1.0\r\n"
396
 					  "User-Agent: gPXE/" VERSION "\r\n"
418
 					  "User-Agent: gPXE/" VERSION "\r\n"
419
+					  "%s%s%s"
397
 					  "Host: %s\r\n"
420
 					  "Host: %s\r\n"
398
 					  "\r\n",
421
 					  "\r\n",
399
 					  ( path ? path : "/" ),
422
 					  ( path ? path : "/" ),
400
 					  ( query ? "?" : "" ),
423
 					  ( query ? "?" : "" ),
401
 					  ( query ? query : "" ),
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
 					  host ) ) != 0 ) {
430
 					  host ) ) != 0 ) {
403
 			http_done ( http, rc );
431
 			http_done ( http, rc );
404
 		}
432
 		}

Loading…
Cancel
Save