Exemplo n.º 1
0
void HTTPReply (
    int nStatus, std::string const& content, Json::Output const& output, beast::Journal j)
{
    JLOG (j.trace())
        << "HTTP Reply " << nStatus << " " << content;

    if (nStatus == 401)
    {
        output ("HTTP/1.0 401 Authorization Required\r\n");
        output (getHTTPHeaderTimestamp ());

        // CHECKME this returns a different version than the replies below. Is
        //         this by design or an accident or should it be using
        //         BuildInfo::getFullVersionString () as well?
        output ("Server: " + systemName () + "-json-rpc/v1");
        output ("\r\n");

        // Be careful in modifying this! If you change the contents you MUST
        // update the Content-Length header as well to indicate the correct
        // size of the data.
        output ("WWW-Authenticate: Basic realm=\"jsonrpc\"\r\n"
                    "Content-Type: text/html\r\n"
                    "Content-Length: 296\r\n"
                    "\r\n"
                    "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 "
                    "Transitional//EN\"\r\n"
                    "\"http://www.w3.org/TR/1999/REC-html401-19991224/loose.dtd"
                    "\">\r\n"
                    "<HTML>\r\n"
                    "<HEAD>\r\n"
                    "<TITLE>Error</TITLE>\r\n"
                    "<META HTTP-EQUIV='Content-Type' "
                    "CONTENT='text/html; charset=ISO-8859-1'>\r\n"
                    "</HEAD>\r\n"
                    "<BODY><H1>401 Unauthorized.</H1></BODY>\r\n");

        return;
    }

    switch (nStatus)
    {
    case 200: output ("HTTP/1.1 200 OK\r\n"); break;
    case 400: output ("HTTP/1.1 400 Bad Request\r\n"); break;
    case 403: output ("HTTP/1.1 403 Forbidden\r\n"); break;
    case 404: output ("HTTP/1.1 404 Not Found\r\n"); break;
    case 500: output ("HTTP/1.1 500 Internal Server Error\r\n"); break;
    case 503: output ("HTTP/1.1 503 Server is overloaded\r\n"); break;
    }

    output (getHTTPHeaderTimestamp ());

    output ("Connection: Keep-Alive\r\n"
            "Content-Length: ");

    // VFALCO TODO Determine if/when this header should be added
    //if (context.app.config().RPC_ALLOW_REMOTE)
    //    output ("Access-Control-Allow-Origin: *\r\n");

    output (std::to_string(content.size () + 2));
    output ("\r\n"
            "Content-Type: application/json; charset=UTF-8\r\n");

    output ("Server: " + systemName () + "-json-rpc/");
    output (BuildInfo::getFullVersionString ());
    output ("\r\n"
            "\r\n");
    output (content);
    output ("\r\n");
}
Exemplo n.º 2
0
std::string HTTPReply (int nStatus, std::string const& strMsg)
{
    if (ShouldLog (lsTRACE, RPC))
    {
        WriteLog (lsTRACE, RPC) << "HTTP Reply " << nStatus << " " << strMsg;
    }

    std::string ret;

    if (nStatus == 401)
    {
        ret.reserve (512);

        ret.append ("HTTP/1.0 401 Authorization Required\r\n");
        ret.append (getHTTPHeaderTimestamp ());

        // CHECKME this returns a different version than the replies below. Is
        //         this by design or an accident or should it be using
        //         BuildInfo::getFullVersionString () as well?
        ret.append ("Server: " SYSTEM_NAME "-json-rpc/");
        ret.append (FormatFullVersion ());
        ret.append ("\r\n");

        // Be careful in modifying this! If you change the contents you MUST
        // update the Content-Length header as well to indicate the correct
        // size of the data.
        ret.append ("WWW-Authenticate: Basic realm=\"jsonrpc\"\r\n"
                    "Content-Type: text/html\r\n"
                    "Content-Length: 296\r\n"
                    "\r\n"
                    "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\r\n"
                    "\"http://www.w3.org/TR/1999/REC-html401-19991224/loose.dtd\">\r\n"
                    "<HTML>\r\n"
                    "<HEAD>\r\n"
                    "<TITLE>Error</TITLE>\r\n"
                    "<META HTTP-EQUIV='Content-Type' CONTENT='text/html; charset=ISO-8859-1'>\r\n"
                    "</HEAD>\r\n"
                    "<BODY><H1>401 Unauthorized.</H1></BODY>\r\n");

        return ret;
    }

    ret.reserve(256 + strMsg.length());

    switch (nStatus)
    {
    case 200:
        ret.append ("HTTP/1.1 200 OK\r\n");
        break;
    case 400:
        ret.append ("HTTP/1.1 400 Bad Request\r\n");
        break;
    case 403:
        ret.append ("HTTP/1.1 403 Forbidden\r\n");
        break;
    case 404:
        ret.append ("HTTP/1.1 404 Not Found\r\n");
        break;
    case 500:
        ret.append ("HTTP/1.1 500 Internal Server Error\r\n");
        break;
    }

    ret.append (getHTTPHeaderTimestamp ());

    ret.append ("Connection: Keep-Alive\r\n");

    if (getConfig ().RPC_ALLOW_REMOTE)
        ret.append ("Access-Control-Allow-Origin: *\r\n");

    ret.append ("Content-Length: ");
    ret.append (std::to_string(strMsg.size () + 2));
    ret.append ("\r\n");

    ret.append ("Content-Type: application/json; charset=UTF-8\r\n");

    ret.append ("Server: " SYSTEM_NAME "-json-rpc/");
    ret.append (BuildInfo::getFullVersionString ());
    ret.append ("\r\n");

    ret.append ("\r\n");
    ret.append (strMsg);
    ret.append ("\r\n");

    return ret;
}