Example #1
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);
}
Example #2
0
int HttpServer::run(void* runArg)
{
    OsConnectionSocket* requestSocket = NULL;

    if (!mpServerSocket->isOk())
    {
        OsSysLog::add( FAC_SIP, PRI_ERR, "HttpServer: port not ok" );
        httpStatus = OS_PORT_IN_USE;
    }

    while(!isShuttingDown() && mpServerSocket->isOk())
    {
        requestSocket = mpServerSocket->accept();

        if(requestSocket)
        {
            if (mbPersistentConnection)
            {
                // Take this opportunity to check for any old HttpConnections that can be deleted
                int items = mpHttpConnectionList->entries();
                if (items != 0)
                {
                    int deleted = 0;

                    UtlSListIterator iterator(*mpHttpConnectionList);
                    HttpConnection* connection;
                    while ((connection = dynamic_cast<HttpConnection*>(iterator())))
                    {
                        if (connection->toBeDeleted())
                        {
                           OsSysLog::add(FAC_SIP, PRI_DEBUG,
                                         "HttpServer: destroying connection %p",
                                         connection);
                           mpHttpConnectionList->destroy(connection);
                           ++deleted;

                           if (mHttpConnections > 0)
                           {
                              --mHttpConnections;
                           }
                        }
                    }
                    items = mpHttpConnectionList->entries();
                    OsSysLog::add(FAC_SIP, PRI_DEBUG,
                                  "HttpServer: "
                                  "destroyed %d inactive HttpConnections, %d remaining",
                                  deleted, items);
                }
                // Create new persistent connection
                if (mHttpConnections < MAX_PERSISTENT_HTTP_CONNECTIONS)
                {
                    ++mHttpConnections;
                    HttpConnection* newConnection = new HttpConnection(requestSocket, this);
                    mpHttpConnectionList->append(newConnection);
                    OsSysLog::add(FAC_SIP, PRI_INFO,
                                  "HttpServer::run starting persistent connection %d (%p)",
                                  mHttpConnections, newConnection);
                    newConnection->start();
                }
                else
                {
                   OsSysLog::add(FAC_SIP, PRI_ERR,
                                 "HttpServer::run exceeded persistent connection limit (%d):"
                                 " sending 503",
                                 MAX_PERSISTENT_HTTP_CONNECTIONS);
                    HttpMessage request;
                    HttpMessage response;
                    // Read the http request from the socket
                    request.read(requestSocket);

                    // Send out-of-resources message
                    response.setResponseFirstHeaderLine(HTTP_PROTOCOL_VERSION,
                                                        HTTP_OUT_OF_RESOURCES_CODE,
                                                        HTTP_OUT_OF_RESOURCES_TEXT);
                    response.write(requestSocket);
                    requestSocket->close();
                    delete requestSocket;
                    requestSocket = NULL;
                }
            }
            else
            {
                HttpMessage request;
                // Read a http request from the socket
                request.read(requestSocket);

                UtlString remoteIp;
                requestSocket->getRemoteHostIp(&remoteIp);

                HttpMessage* response = NULL;

                // If request from Valid IP Address
                if( processRequestIpAddr(remoteIp, request, response))
                {
                   // If the request is authorized
                   processRequest(request, response, requestSocket);
                }

                if(response)
                {
                    response->write(requestSocket);
                    delete response;
                    response = NULL;
                }

                requestSocket->close();
                delete requestSocket;
                requestSocket = NULL;
            }
        }
        else
        {
           httpStatus = OS_PORT_IN_USE;
        }
    } // while (!isShuttingDown && mpServerSocket->isOk())

    if ( !isShuttingDown() )
    {
       OsSysLog::add( FAC_SIP, PRI_ERR, "HttpServer: exit due to port failure" );
    }

    httpStatus = OS_TASK_NOT_STARTED;

    return(TRUE);
}