[http] Include a zero request content length when applicable

We currently send a Content-Length request header only when the
content length is non-zero.  This breaks some use cases for a PUT
request, such as uploading a zero-length file to AWS S3.

Fix by choosing whether or not to include a Content-Length request
header based on the request method.

Signed-off-by: Michael Brown <mcb30@ipxe.org>
This commit is contained in:
Michael Brown
2026-10-05 15:33:23 +01:00
parent 6262f1081f
commit 8865571f70
2 changed files with 15 additions and 2 deletions
+5
View File
@@ -100,8 +100,13 @@ struct http_connection {
struct http_method {
/** Method name (e.g. "GET" or "POST") */
const char *name;
/** Flags */
unsigned int flags;
};
/** HTTP method has a request body */
#define HTTP_FL_REQ_BODY 0x0001
/** HTTP method table */
#define HTTP_METHODS __table ( struct http_method, "http_methods" )
+10 -2
View File
@@ -138,21 +138,25 @@ static struct http_transfer_encoding http_transfer_identity;
/** HTTP HEAD method */
struct http_method http_head __http_method = {
.name = "HEAD",
.flags = 0,
};
/** HTTP GET method */
struct http_method http_get __http_method = {
.name = "GET",
.flags = 0,
};
/** HTTP POST method */
struct http_method http_post __http_method = {
.name = "POST",
.flags = HTTP_FL_REQ_BODY,
};
/** HTTP PUT method */
struct http_method http_put __http_method = {
.name = "PUT",
.flags = HTTP_FL_REQ_BODY,
};
/**
@@ -1095,11 +1099,15 @@ static int http_format_content_length ( struct http_transaction *http,
char *buf, size_t len ) {
/* Construct content length, if applicable */
if ( http->request.content.len ) {
if ( http->request.method->flags & HTTP_FL_REQ_BODY ) {
return snprintf ( buf, len, "%zd", http->request.content.len );
} else {
} else if ( http->request.content.len == 0 ) {
return 0;
}
DBGC ( http, "HTTP %p cannot %s with a request body\n",
http, http->request.method->name );
return -ENOTSUP;
}
/** HTTP "Content-Length" header */