Esempio n. 1
0
    void testCreator()
    {
        const char *hdr = "\r\nContent-Length: 0\r\n\r\n";
        ssize_t expectedLen = strlen(hdr);
        HttpMessage* msg = new HttpMessage();
        UtlString buf;
        ssize_t bufLen = 0;

        msg->getBytes(&buf, &bufLen);
        ASSERT_STR_EQUAL_MESSAGE("Set header comes back intact", hdr, (char *)buf.data());
        CPPUNIT_ASSERT_MESSAGE("bytes should not be null", !buf.isNull());
        CPPUNIT_ASSERT_EQUAL_MESSAGE("buffer should contain only content length field",
                expectedLen, bufLen);
        ASSERT_STR_EQUAL_MESSAGE("buffer should contain only content length field",
                buf.data(), hdr);
        delete msg;
    }
Esempio n. 2
0
int HttpGetCommand::execute(int argc, char* argv[])
{
    int commandStatus = CommandProcessor::COMMAND_FAILED;
    if(argc == 2)
    {
        commandStatus = CommandProcessor::COMMAND_SUCCESS;
        const char* url = argv[1];
        const char* serverBegin = strstr(url, "http://");
        if(serverBegin != url)
        {
            printf("unsupported protocol in Url: %s\n",
                url);
                    commandStatus = CommandProcessor::COMMAND_BAD_SYNTAX;
        }

        else
        {
            serverBegin += 7;
            UtlString uri(serverBegin);
            int serverEndIndex = uri.index("/");
            if(serverEndIndex < 0) serverEndIndex = uri.length();
            if(serverEndIndex > 0)
            {
                UtlString server = uri;
                server.remove(serverEndIndex);
                int portIndex = server.index(":");
                int port = PORT_NONE;
                if(portIndex > 0)
                {
                    UtlString portString = server;
                    server.remove(portIndex);
                    portString.remove(0, portIndex + 1);
                    printf("port string: %s\n", portString.data());
                    port = atoi(portString.data());
                }
                uri.remove(0, serverEndIndex);
                if(uri.isNull()) uri = "/";
                printf("HTTP get of %s from server %s port: %d\n",
                    uri.data(), server.data(), port);

                if (!portIsValid(port))
                {
                    port = 80;
                    printf("defaulting to http port 80\n");
                }

                OsConnectionSocket getSocket(port, server.data());
                HttpMessage getRequest;
                getRequest.setFirstHeaderLine("GET", uri.data(), HTTP_PROTOCOL_VERSION);

                int wroteBytes = getRequest.write(&getSocket);
                printf("wrote %d\n", wroteBytes);

                HttpMessage getResponse;
                getResponse.read(&getSocket);

                UtlString responseBytes;
                int responseLength;
                getResponse.getBytes(&responseBytes, &responseLength);
                printf("Got %d bytes\n", responseLength);
                printf("Response: ++++++++++++++++++++++++++++++++++\n%s\n",
                    responseBytes.data());
            }

            else
            {
                printf("invalid server in Url: %s\n",
                url);
                        commandStatus = CommandProcessor::COMMAND_BAD_SYNTAX;
            }
        }
    }

        else
        {
                UtlString usage;
                getUsage(argv[0], &usage);
                printf("%s", usage.data());
                commandStatus = CommandProcessor::COMMAND_BAD_SYNTAX;
                //commandStatus = CommandProcessor::COMMAND_FAILED;
        }

        return(commandStatus);
}
Esempio n. 3
0
/* ============================ ACCESSORS ================================= */
void
WebServer::ProcessEvent(
    const HttpRequestContext& requestContext,
    const HttpMessage& request,
    HttpMessage*& response )
{
    // get the action type (used to be the event)
    UtlString event;
    response = new HttpMessage();

    ssize_t len;
    UtlString httpString;
    SubscribeServerPluginBase* plugin = NULL;

    request.getBytes(&httpString , &len);
    Os::Logger::instance().log(FAC_SIP, PRI_INFO,
                  "WebServer::ProcessEvent HttpEvent \n%s",
                  httpString.data());

    // get the ACTION CGI variable
    requestContext.getCgiVariable( EVENTTYPE, event );

    if( !event.isNull())
    {
        //according to event type , get the correct plugin from spPluginTable
        StatusPluginReference* pluginContainer = spPluginTable->getPlugin(event);
        if(pluginContainer)
        {
            plugin = pluginContainer->getPlugin();
            if(plugin)
            {
                // send 200 ok reply.
                response->setResponseFirstHeaderLine (
                    HTTP_PROTOCOL_VERSION,
                    HTTP_OK_CODE,
                    HTTP_OK_TEXT );

                //call the event handler for the plugin
                plugin->handleEvent(requestContext, request, *response);
            }
            else
            {
               Os::Logger::instance().log(FAC_SIP, PRI_ERR,
                             "WebServer::ProcessEvent no plugin in container for event type '%s'",
                             event.data()
                             );
            }
        }
        else
        {
           Os::Logger::instance().log(FAC_SIP, PRI_WARNING,
                         "WebServer::ProcessEvent no plugin found for event type '%s'",
                         event.data()
                         );
        }
    }
    else
    {
       Os::Logger::instance().log(FAC_SIP, PRI_WARNING,
                     "WebServer::ProcessEvent no '" EVENTTYPE "' variable found"
                     );
    }


    // We did not find a plugin so nobody handled this request.
    if(plugin == NULL)
    {
        response->setResponseFirstHeaderLine (
            HTTP_PROTOCOL_VERSION,
            HTTP_FILE_NOT_FOUND_CODE,
            HTTP_FILE_NOT_FOUND_TEXT );
    }
    Os::Logger::instance().flush();

}
Esempio n. 4
0
    /**
     * Test header, message, body, message contructor
     */
    void testMessage()
    {
        // TODO break this up into several tests. Too intertwined
        const char* name = "Content-Type";
        const char* value = "text/plain";
        const char* httpTopLine = "GET /index.html HTTP/1.0";
        const char* valueRef = NULL;
        const char* n2 = "yyy";
        const char* v2 = "yyy-value";
        const char* v2a = "yyy-value2";
        UtlString messageBytes;
        UtlString messageBytes2;
        ssize_t messageLen = 0;
        ssize_t messageLen2 = 0;
        const char* body = "<HTML>\n<H3>Hello\n<BR>\n</HTML>\n";
        const HttpBody *bodyRef;
        ssize_t bodyLength = 0;
        UtlString headerLinePart;
        HttpMessage *msg;
        HttpMessage *msg2;

        msg = new HttpMessage();

        // H E A D E R
        int fieldCount = msg->getCountHeaderFields();
        CPPUNIT_ASSERT_EQUAL_MESSAGE("field count should be zero",
                0, fieldCount);

        msg->addHeaderField(name, value);
        fieldCount = msg->getCountHeaderFields();
        CPPUNIT_ASSERT_EQUAL_MESSAGE("field count should be zero", 1,
                fieldCount);

        valueRef = msg->getHeaderValue(0);
        CPPUNIT_ASSERT_MESSAGE("NULL field value", valueRef != NULL);
        ASSERT_STR_EQUAL_MESSAGE("incorrect field value", value,
                valueRef);

        msg->setFirstHeaderLine(httpTopLine);
        valueRef = msg->getFirstHeaderLine();
        ASSERT_STR_EQUAL_MESSAGE("incorrect top header line value",
                valueRef, httpTopLine);

        valueRef = msg->getHeaderValue(0, name);
        CPPUNIT_ASSERT_MESSAGE("NULL field value", valueRef != NULL);
        ASSERT_STR_EQUAL_MESSAGE("incorrect field value", value,
            valueRef);

        msg->addHeaderField(n2, v2);
        fieldCount = msg->getCountHeaderFields();
        CPPUNIT_ASSERT_EQUAL_MESSAGE("field count should be 2", 2,
                fieldCount);

        valueRef = msg->getHeaderValue(0, n2);
        CPPUNIT_ASSERT_MESSAGE("NULL field value", valueRef != NULL);
        ASSERT_STR_EQUAL_MESSAGE("incorrect field value", v2,
            valueRef);

        msg->addHeaderField(n2, v2a);
        fieldCount = msg->getCountHeaderFields();
        CPPUNIT_ASSERT_EQUAL_MESSAGE("field count should be 3", 3,
                fieldCount);

        valueRef = msg->getHeaderValue(1, n2);
        CPPUNIT_ASSERT_MESSAGE("NULL field value", valueRef != NULL);
        ASSERT_STR_EQUAL_MESSAGE("incorrect field value", v2a,
            valueRef);

        // B O D Y
        HttpBody *httpBody = new HttpBody(body, strlen(body));
        msg->setBody(httpBody);
        bodyRef = msg->getBody();
        CPPUNIT_ASSERT_MESSAGE("bad body pointer", httpBody == bodyRef);

        bodyRef->getBytes(&valueRef, &bodyLength);
        CPPUNIT_ASSERT_MESSAGE("bad body pointer", valueRef != NULL);
        CPPUNIT_ASSERT_EQUAL_MESSAGE("incorrect body len", (ssize_t)strlen(body),
                bodyLength);
        ASSERT_STR_EQUAL_MESSAGE("incorrect body value", body, valueRef);

        const char* expectedLinePart[] = {
            "GET", "/index.html", "HTTP/1.0"
        };
        size_t n = sizeof(expectedLinePart) / sizeof(expectedLinePart[0]);

        for (size_t i = 0; i < n; i++)
        {
            msg->getFirstHeaderLinePart(i, &headerLinePart);
            CPPUNIT_ASSERT_MESSAGE("NULL header line part pointer",
                !headerLinePart.isNull());
            ASSERT_STR_EQUAL_MESSAGE("incorrect hdr line", expectedLinePart[i],
                headerLinePart.data());
            headerLinePart.remove(0);
        }

        msg->getBytes(&messageBytes, &messageLen);
        CPPUNIT_ASSERT_MESSAGE("NULL body pointer", !messageBytes.isNull());

        // message constructor
        msg2 = new HttpMessage(messageBytes.data(), messageLen);
        msg2->getBytes(&messageBytes2, &messageLen2);
        valueRef = msg2->getHeaderValue(0, name);
        ASSERT_STR_EQUAL_MESSAGE("incorrect message bytes", value, valueRef);
        CPPUNIT_ASSERT_EQUAL_MESSAGE("incorrect message byte length",
                messageLen, messageLen2);
        delete msg2;

        delete msg;

        // AS DESIGNED: body delete is handled by delete msg
        // delete httpBody;
    }
Esempio n. 5
0
void XmlRpcDispatch::processRequest(const HttpRequestContext& requestContext,
                                    const HttpMessage& request,
                                    HttpMessage*& response )
{
#   ifdef TEST_HTTP
    ssize_t len;
    UtlString httpString;

    request.getBytes(&httpString , &len);
    OsSysLog::add(FAC_XMLRPC, PRI_DEBUG,
                  "XmlRpcDispatch::processRequest HttpEvent = \n%s",
                  httpString.data());
#  endif
    
   // Create a response
   response = new HttpMessage();
   response->setResponseFirstHeaderLine(HTTP_PROTOCOL_VERSION_1_1,
                                        HTTP_OK_CODE,
                                        HTTP_OK_TEXT);

   UtlString bodyString;
   ssize_t bodyLength;
   const HttpBody* requestBody = request.getBody();
   requestBody->getBytes(&bodyString, &bodyLength);
   
   XmlRpcMethod::ExecutionStatus status;
   UtlString methodName("<unparsed>");
   XmlRpcResponse responseBody;
   XmlRpcMethodContainer* methodContainer = NULL;
   UtlSList params;

   UtlString logString;
   if (bodyString.length() > XmlRpcBody::MAX_LOG)
   {
      logString.append(bodyString, 0, XmlRpcBody::MAX_LOG);
      logString.append("\n...");
   }
   else
   {
      logString = bodyString;
   }
   OsSysLog::add(FAC_XMLRPC, PRI_INFO,
                 "XmlRpcDispatch::processRequest requestBody = \n%s",
                 logString.data());
   
   if (parseXmlRpcRequest(bodyString, methodContainer, params, responseBody))
   {
      if (methodContainer)
      {
         methodContainer->getName(methodName);
         responseBody.setMethod(methodName);
      
         XmlRpcMethod::Get* methodGet;
         void* userData;
         methodContainer->getData(methodGet, userData);
         XmlRpcMethod* method = methodGet();
         OsSysLog::add(FAC_XMLRPC, PRI_DEBUG,
                       "XmlRpcDispatch::processRequest calling method '%s'",
                       methodName.data()
                       );
         method->execute(requestContext,
                         params, 
                         userData,
                         responseBody,
                         status
                         );
      
         // Delete the instance of the method                
         delete method;

         // Clean up the memory allocated in params
         XmlRpcBody::deallocateContainedValues(&params);

         // if the method wants authentication, build the standard response
         // for anything else, it's already built one.
         if (XmlRpcMethod::REQUIRE_AUTHENTICATION == status)
         {
            // Create an authentication challenge response
            responseBody.setFault(AUTHENTICATION_REQUIRED_FAULT_CODE,
                                  AUTHENTICATION_REQUIRED_FAULT_STRING);
         }
      }
      else
      {
         // could not find a registered method - logged and response built in parseXmlRpcRequest
         status = XmlRpcMethod::FAILED;         
      }
   }
   else
   {
      // Parsing the request failed - it will have logged a specific error a
      // and created an appropriate response body
      status = XmlRpcMethod::FAILED;
   }

   // Send the response back
   responseBody.getBody()->getBytes(&bodyString, &bodyLength);

   logString.remove(0);
   if (bodyString.length() > XmlRpcBody::MAX_LOG)
   {
      logString.append(bodyString, 0, XmlRpcBody::MAX_LOG);
      logString.append("\n...");
   }
   else
   {
      logString = bodyString;
   }
   
   OsSysLog::add(FAC_XMLRPC, PRI_INFO,
                 "XmlRpcDispatch::processRequest method '%s' response status=%s\n%s",
                 methodName.data(),
                 XmlRpcMethod::ExecutionStatusString(status),
                 logString.data()
                 );
   
   response->setBody(new HttpBody(bodyString.data(), bodyLength));
   response->setContentType(CONTENT_TYPE_TEXT_XML);
   response->setContentLength(bodyLength);
}