Exemplo n.º 1
0
char *cg_upnp_ssdpresponse_tostring(CgUpnpSSDPResponse *ssdpRes, CgString *ssdpMsg)
{
    CgHttpHeader *header;
    char statusCodeBuf[CG_STRING_INTEGER_BUFLEN];
    const char *name;
    const char *value;

    cg_log_debug_l4("Entering...\n");

    cg_string_addvalue(ssdpMsg, cg_http_response_getversion(ssdpRes));
    cg_string_addvalue(ssdpMsg, CG_HTTP_SP);
    cg_string_addvalue(ssdpMsg, cg_int2str(cg_http_response_getstatuscode(ssdpRes), statusCodeBuf, sizeof(statusCodeBuf)));
    cg_string_addvalue(ssdpMsg, CG_HTTP_SP);
    cg_string_addvalue(ssdpMsg, cg_http_response_getreasonphrase(ssdpRes));
    cg_string_addvalue(ssdpMsg, CG_HTTP_CRLF);

    for (header = cg_http_packet_getheaders((CgHttpPacket *)ssdpRes); header != NULL; header = cg_http_header_next(header)) {
        name = cg_http_header_getname(header);
        value = cg_http_header_getvalue(header);
        cg_string_addvalue(ssdpMsg, name);
        cg_string_addvalue(ssdpMsg, CG_HTTP_COLON);
        cg_string_addvalue(ssdpMsg, CG_HTTP_SP);
        cg_string_addvalue(ssdpMsg, value);
        cg_string_addvalue(ssdpMsg, CG_HTTP_CRLF);
    }
    cg_string_addvalue(ssdpMsg, CG_HTTP_CRLF);

    return cg_string_getvalue(ssdpMsg);

    cg_log_debug_l4("Leaving...\n");
}
Exemplo n.º 2
0
CgSoapResponse *cg_soap_request_post(CgSoapRequest *soapReq, const char *ipaddr, int port)
{
    CgHttpResponse *httpRes;
    char *content;
    size_t contentLen;
    CgXmlParser *xmlParser;
    CgHttpHeader *header = NULL;

    cg_log_debug_l4("Entering...\n");

    httpRes = cg_http_request_post(soapReq->httpReq, ipaddr, port);

    /* Check for HTTP response 405 Method Not Allowed */
    if (cg_http_response_getstatuscode(httpRes) == CG_HTTP_STATUS_METHOD_NOT_ALLOWED)
    {
        /* Status code implies that we need to use M-POST */
        cg_http_request_setmethod(soapReq->httpReq, CG_HTTP_MPOST);

        /* Add MAN header */
        header = cg_http_header_new();
        cg_http_header_setname(header, CG_HTTP_MAN);
        cg_http_header_setvalue(header, CG_HTTP_SOAP_MAN_VALUE);
        cg_http_packet_addheader((CgHttpPacket*)soapReq->httpReq, header);

        /* Change soapaction header name to include namespace */
        header = cg_http_packet_getheader((CgHttpPacket*)soapReq->httpReq,
                          CG_HTTP_SOAP_ACTION);
        if (header != NULL)
        {
            cg_http_header_setname(header, CG_HTTP_SOAP_ACTION_WITH_NS);
        }

        /* New attempt */
        httpRes = cg_http_request_post(soapReq->httpReq, ipaddr, port);
    }

    cg_soap_response_sethttpresponse(soapReq->soapRes,httpRes);

    content = cg_http_response_getcontent(httpRes);
    contentLen = cg_http_response_getcontentlength(httpRes);
    if (content == NULL || contentLen <= 0)
        return soapReq->soapRes;

    xmlParser = cg_xml_parser_new();
    cg_xml_parse(xmlParser, soapReq->soapRes->rootNodeList, content, contentLen);
    cg_xml_parser_delete(xmlParser);

    cg_log_debug_l4("Leaving...\n");

    return soapReq->soapRes;
}
Exemplo n.º 3
0
BOOL cg_http_request_postresponse(CgHttpRequest *httpReq, CgHttpResponse *httpRes)
{
    CgSocket *sock;
    char httpDate[CG_HTTP_DATE_MAXLEN];
    char *version, *reasonPhrase;
    int statusCode;
    char statusCodeBuf[CG_STRING_INTEGER_BUFLEN];

    cg_log_debug_l4("Entering...\n");

    sock = cg_http_request_getsocket(httpReq);

    cg_log_debug_s("Got request:\n");
    cg_http_request_print(httpReq);

    cg_http_response_setdate(httpRes, cg_http_getdate(cg_getcurrentsystemtime(), httpDate, sizeof(httpDate)));

    version = cg_http_response_getversion(httpRes);
    statusCode = cg_http_response_getstatuscode(httpRes);
    reasonPhrase = cg_http_response_getreasonphrase(httpRes);

    if (version == NULL || reasonPhrase == NULL)
        return FALSE;

    cg_int2str(statusCode, statusCodeBuf, sizeof(statusCodeBuf));

    /**** send first line ****/
    cg_socket_write(sock, version, cg_strlen(version));
    cg_socket_write(sock, CG_HTTP_SP, sizeof(CG_HTTP_SP)-1);
    cg_socket_write(sock, statusCodeBuf, cg_strlen(statusCodeBuf));
    cg_socket_write(sock, CG_HTTP_SP, sizeof(CG_HTTP_SP)-1);
    cg_socket_write(sock, reasonPhrase, cg_strlen(reasonPhrase));
    cg_socket_write(sock, CG_HTTP_CRLF, sizeof(CG_HTTP_CRLF)-1);

    cg_log_debug_s("Posting response:\n");
    cg_http_response_print(httpRes);

    /**** send header and content ****/
    cg_http_packet_post((CgHttpPacket *)httpRes, sock);

    cg_log_debug_l4("Leaving...\n");

    return TRUE;
}