From ab9213a4ecbe19e9ecdee73bbbf2ea1300fc917e Mon Sep 17 00:00:00 2001 From: Karen Etheridge Date: Mon, 6 Oct 2025 15:33:04 -0700 Subject: [PATCH] properly support an HTTP method of '0' This is valid according to RFC9110: see https://datatracker.ietf.org/doc/html/rfc9110#appendix-A for the full ABNF, which is equivalent to the regex: ^[a-zA-Z0-9!#$%&'*+.^_`|~-]+$ This also matches the check in LWP::Protocol::http::request. --- Changes | 1 + lib/HTTP/Request.pm | 5 +++-- t/request.t | 12 +++++++++++- 3 files changed, 15 insertions(+), 3 deletions(-) diff --git a/Changes b/Changes index fffba9c8..5e6074ec 100644 --- a/Changes +++ b/Changes @@ -1,6 +1,7 @@ Revision history for HTTP-Message {{$NEXT}} + - now handling HTTP method '0' (Karen Etheridge) 7.00 2024-10-07 15:31:56Z - Stop transforming LF into CRLF. Fixes #69 (GH#196) (Olaf Alders) diff --git a/lib/HTTP/Request.pm b/lib/HTTP/Request.pm index f4f483a9..43ee1d40 100644 --- a/lib/HTTP/Request.pm +++ b/lib/HTTP/Request.pm @@ -118,7 +118,8 @@ sub as_string my($eol) = @_; $eol = "\n" unless defined $eol; - my $req_line = $self->method || "-"; + # method must be at least one char, matching ^[a-zA-Z0-9!#$%&'*+.^_`|~-]+$ + my $req_line = (length $self->method) ? $self->method : "-"; my $uri = $self->uri; $uri = (defined $uri) ? $uri->as_string : "-"; $req_line .= " $uri"; @@ -131,7 +132,7 @@ sub as_string sub dump { my $self = shift; - my @pre = ($self->method || "-", $self->uri || "-"); + my @pre = ((length $self->method) ? $self->method : "-", (defined $self->uri) ? $self->uri : "-"); if (my $prot = $self->protocol) { push(@pre, $prot); } diff --git a/t/request.t b/t/request.t index c9d868b2..955d06a7 100644 --- a/t/request.t +++ b/t/request.t @@ -5,7 +5,7 @@ use strict; use warnings; use Test::More; -plan tests => 39; +plan tests => 43; use HTTP::Request; use Try::Tiny qw( catch try ); @@ -166,3 +166,13 @@ $r2 = HTTP::Request->parse('methonly http://www.example.com/'); is( $r2->method, 'methonly' ); is( $r2->uri, 'http://www.example.com/' ); is( $r2->protocol, undef ); + +my $r3 = HTTP::Request->new(0 => "0"); +is($r3->method, '0', '0 is a valid HTTP method'); +is($r3->uri, '0', '0 is a valid URI'); +is($r3->as_string, "0 0\n\n", 'parsed zero method, zero uri req'); +is($r3->dump, <