From 7696f7dd5492c8553f1c81a7ac27c4c4403dbcaa Mon Sep 17 00:00:00 2001 From: Adrien Destugues Date: Tue, 15 Oct 2013 14:43:09 +0200 Subject: [PATCH] HttpRequest: allow custom http methods * The W3C XmlHttpRequest testsuite likes to use "CHICKEN" as a method. * Also add constants for all specified methods in HTTP 1.1. --- headers/os/net/HttpRequest.h | 20 ++++++------ src/kits/network/libnetapi/HttpRequest.cpp | 38 +++------------------- 2 files changed, 15 insertions(+), 43 deletions(-) diff --git a/headers/os/net/HttpRequest.h b/headers/os/net/HttpRequest.h index cb65e1e002..c88c4dea87 100644 --- a/headers/os/net/HttpRequest.h +++ b/headers/os/net/HttpRequest.h @@ -29,7 +29,7 @@ public: BUrlContext* context = NULL); virtual ~BHttpRequest(); - void SetMethod(int8 method); + void SetMethod(const char* const method); void SetFollowLocation(bool follow); void SetMaxRedirections(int8 maxRedirections); void SetReferrer(const BString& referrer); @@ -74,7 +74,7 @@ private: BNetworkAddress fRemoteAddr; bool fSSL; - int8 fRequestMethod; + BString fRequestMethod; int8 fHttpVersion; BString fOutputBuffer; @@ -123,14 +123,14 @@ enum { // Request method -enum { - B_HTTP_GET = 1, - B_HTTP_POST, - B_HTTP_PUT, - B_HTTP_HEAD, - B_HTTP_DELETE, - B_HTTP_OPTIONS -}; +const char* const B_HTTP_GET = "GET"; +const char* const B_HTTP_POST = "POST"; +const char* const B_HTTP_PUT = "PUT"; +const char* const B_HTTP_HEAD = "HEAD"; +const char* const B_HTTP_DELETE = "DELETE"; +const char* const B_HTTP_OPTIONS = "OPTIONS"; +const char* const B_HTTP_TRACE = "TRACE"; +const char* const B_HTTP_CONNECT = "CONNECT"; // HTTP Version diff --git a/src/kits/network/libnetapi/HttpRequest.cpp b/src/kits/network/libnetapi/HttpRequest.cpp index 796764dfe1..39b6543834 100644 --- a/src/kits/network/libnetapi/HttpRequest.cpp +++ b/src/kits/network/libnetapi/HttpRequest.cpp @@ -58,7 +58,7 @@ BHttpRequest::~BHttpRequest() void -BHttpRequest::SetMethod(int8 method) +BHttpRequest::SetMethod(const char* const method) { fRequestMethod = method; } @@ -265,7 +265,8 @@ BHttpRequest::_ProtocolLoop() if (!_ResolveHostName()) { _EmitDebug(B_URL_PROTOCOL_DEBUG_ERROR, - "Unable to resolve hostname, aborting."); + "Unable to resolve hostname (%s), aborting.", + fUrl.Host().String()); return B_PROT_CANT_RESOLVE_HOSTNAME; } @@ -732,22 +733,7 @@ BHttpRequest::_ParseHeaders() void BHttpRequest::_CreateRequest() { - BString request; - - switch (fRequestMethod) { - case B_HTTP_POST: - request << "POST"; - break; - - case B_HTTP_PUT: - request << "PUT"; - break; - - default: - case B_HTTP_GET: - request << "GET"; - break; - } + BString request(fRequestMethod); if (Url().HasPath()) request << ' ' << Url().Path(); @@ -804,21 +790,7 @@ BHttpRequest::_AddHeaders() // Authentication if (fAuthentication.Method() != B_HTTP_AUTHENTICATION_NONE) { - BString request; - switch (fRequestMethod) { - case B_HTTP_POST: - request = "POST"; - break; - - case B_HTTP_PUT: - request = "PUT"; - break; - - default: - case B_HTTP_GET: - request = "GET"; - break; - } + BString request(fRequestMethod); fOutputHeaders.AddHeader("Authorization", fAuthentication.Authorization(fUrl, request));