Skip to content

Commit

Permalink
Merge pull request #735 from mpatrascoiu/XrdHttpErrorMap
Browse files Browse the repository at this point in the history
XrdHttp: Xrd error code to HTTP status translation
To me it works and compiles on the main platforms
  • Loading branch information
ffurano committed Jun 13, 2018
2 parents 6e788cb + 62a996d commit 0f1c657
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 17 deletions.
7 changes: 5 additions & 2 deletions src/XrdHttp/XrdHttpProtocol.cc
Expand Up @@ -1426,9 +1426,12 @@ int XrdHttpProtocol::StartSimpleResp(int code, const char *desc, const char *hea
ss << desc;
} else {
if (code == 200) ss << "OK";
else if (code == 206) ss << "Partial content";
else if (code == 206) ss << "Partial Content";
else if (code == 302) ss << "Redirect";
else if (code == 404) ss << "Not found";
else if (code == 403) ss << "Forbidden";
else if (code == 404) ss << "Not Found";
else if (code == 405) ss << "Method Not Allowed";
else if (code == 500) ss << "Internal Server Error";
else ss << "Unknown";
}
ss << crlf;
Expand Down
69 changes: 54 additions & 15 deletions src/XrdHttp/XrdHttpReq.cc
Expand Up @@ -770,6 +770,38 @@ void XrdHttpReq::parseResource(char *res) {

}

// Map an XRootD error code to an appropriate HTTP status code and message
// The variables httpStatusCode and httpStatusText will be populated

void XrdHttpReq::mapXrdErrorToHttpStatus() {
// Set default HTTP status values for an error case
httpStatusCode = 500;
httpStatusText = "Unrecognized error";

// Do error mapping
if (xrdresp == kXR_error) {
switch (xrderrcode) {
case kXR_NotAuthorized:
httpStatusCode = 403; httpStatusText = "Operation not permitted";
break;
case kXR_NotFound:
httpStatusCode = 404; httpStatusText = "File not found";
break;
case kXR_Unsupported:
httpStatusCode = 405; httpStatusText = "Operation not supported";
default:
break;
}

if (!etext.empty()) httpStatusText = etext;

TRACEI(REQ, "PostProcessHTTPReq mapping Xrd error [" << xrderrcode
<< "] to status code [" << httpStatusCode << "]");

httpStatusText += "\n";
}
}

int XrdHttpReq::ProcessHTTPReq() {

kXR_int32 l;
Expand Down Expand Up @@ -1419,6 +1451,7 @@ int XrdHttpReq::ProcessHTTPReq() {
int XrdHttpReq::PostProcessHTTPReq(bool final_) {

TRACEI(REQ, "PostProcessHTTPReq req: " << request << " reqstate: " << reqstate);
mapXrdErrorToHttpStatus();

switch (request) {
case XrdHttpReq::rtUnknown:
Expand Down Expand Up @@ -1452,11 +1485,13 @@ int XrdHttpReq::PostProcessHTTPReq(bool final_) {
return 1;
}

prot->SendSimpleResp(500, NULL, NULL, NULL, 0);
prot->SendSimpleResp(httpStatusCode, NULL, NULL,
httpStatusText.c_str(), httpStatusText.length());
reset();
return 1;
} else {
prot->SendSimpleResp(404, NULL, NULL, (char *) "Error man!", 0);
prot->SendSimpleResp(httpStatusCode, NULL, NULL,
httpStatusText.c_str(), httpStatusText.length());
return -1;
}
}
Expand All @@ -1467,7 +1502,8 @@ int XrdHttpReq::PostProcessHTTPReq(bool final_) {


if (xrdresp == kXR_error) {
prot->SendSimpleResp(404, NULL, NULL, (char *) etext.c_str(), 0);
prot->SendSimpleResp(httpStatusCode, NULL, NULL,
httpStatusText.c_str(), httpStatusText.length());
return -1;
}

Expand Down Expand Up @@ -1695,7 +1731,8 @@ int XrdHttpReq::PostProcessHTTPReq(bool final_) {
// We are here if the request failed

if (prot->myRole == kXR_isManager) {
prot->SendSimpleResp(404, NULL, NULL, (char *) "File not found.", 0);
prot->SendSimpleResp(httpStatusCode, NULL, NULL,
httpStatusText.c_str(), httpStatusText.length());
return -1;
}

Expand Down Expand Up @@ -1785,8 +1822,8 @@ int XrdHttpReq::PostProcessHTTPReq(bool final_) {
// reqstate--;
// return 0;
//}

prot->SendSimpleResp(404, NULL, NULL, (char *) "Error man!", 0);
prot->SendSimpleResp(httpStatusCode, NULL, NULL,
httpStatusText.c_str(), httpStatusText.length());
return -1;
}

Expand All @@ -1796,7 +1833,7 @@ int XrdHttpReq::PostProcessHTTPReq(bool final_) {
}
default: //read or readv
{

// Nothing to do if we are postprocessing a close
if (ntohs(xrdreq.header.requestid) == kXR_close) return 1;

Expand Down Expand Up @@ -1923,7 +1960,8 @@ int XrdHttpReq::PostProcessHTTPReq(bool final_) {
prot->SendSimpleResp(200, NULL, NULL, (char *) ":-)", 0);
return 1;
} else {
prot->SendSimpleResp(500, NULL, NULL, (char *) etext.c_str(), 0);
prot->SendSimpleResp(httpStatusCode, NULL, NULL,
httpStatusText.c_str(), httpStatusText.length());
return -1;
}
}
Expand All @@ -1944,7 +1982,8 @@ int XrdHttpReq::PostProcessHTTPReq(bool final_) {
{

if (xrdresp != kXR_ok) {
prot->SendSimpleResp(404, NULL, NULL, (char *) etext.c_str(), 0);
prot->SendSimpleResp(httpStatusCode, NULL, NULL,
httpStatusText.c_str(), httpStatusText.length());
return -1;
}

Expand Down Expand Up @@ -1976,7 +2015,8 @@ int XrdHttpReq::PostProcessHTTPReq(bool final_) {
prot->SendSimpleResp(200, NULL, NULL, (char *) ":-)", 0);
return 1;
}
prot->SendSimpleResp(500, NULL, NULL, (char *) "Internal Error", 0);
prot->SendSimpleResp(httpStatusCode, NULL, NULL,
httpStatusText.c_str(), httpStatusText.length());
return -1;
}
}
Expand All @@ -1988,7 +2028,8 @@ int XrdHttpReq::PostProcessHTTPReq(bool final_) {
{

if (xrdresp == kXR_error) {
prot->SendSimpleResp(404, NULL, NULL, (char *) etext.c_str(), 0);
prot->SendSimpleResp(httpStatusCode, NULL, NULL,
httpStatusText.c_str(), httpStatusText.length());
return -1;
}

Expand Down Expand Up @@ -2226,12 +2267,10 @@ int XrdHttpReq::PostProcessHTTPReq(bool final_) {
}





switch (xrdresp) {
case kXR_error:
prot->SendSimpleResp(500, NULL, NULL, (char *) etext.c_str(), 0);
prot->SendSimpleResp(httpStatusCode, NULL, NULL,
httpStatusText.c_str(), httpStatusText.length());
return -1;
break;

Expand Down
6 changes: 6 additions & 0 deletions src/XrdHttp/XrdHttpReq.hh
Expand Up @@ -76,6 +76,10 @@ class XrdOucEnv;

class XrdHttpReq : public XrdXrootd::Bridge::Result {
private:
// HTTP response parameters to be sent back to the user
int httpStatusCode;
std::string httpStatusText;

int parseContentRange(char *);
int parseHost(char *);
int parseRWOp(char *);
Expand All @@ -93,6 +97,8 @@ private:

// Parse a resource string, typically a filename, setting the resource field and the opaque data
void parseResource(char *url);
// Map an XRootD error code to an appropriate HTTP status code and message
void mapXrdErrorToHttpStatus();
public:

XrdHttpReq(XrdHttpProtocol *protinstance) {
Expand Down

0 comments on commit 0f1c657

Please sign in to comment.