如何使用libcurl从HTTP头获取cookie id

时间:2015-06-04 08:45:54

标签: c http cookies libcurl

我的服务器的HTTP haeder是这样的:

Server: Apache-Coyote/1.1
Set-Cookie: JSESSIONID=72D8DC23BD37016CD5557BB353DD008E; Path=/tt/; HttpOnly
SOAPAction: 
Content-Type: text/xml;charset=ISO-8859-1
Content-Length: 491
Date: Wed, 03 Jun 2015 14:02:53 GMT

如何获取cookies id" JSESSIONID = 72D8DC23BD37016CD5557BB353DD008E"然后由我的http客户端使用它。

我使用这些函数初始化http标头并发送消息:

int http_client_init(struct cmp *cmp)
{
#ifdef HTTP_CURL
    http_c.header_list = NULL;
    http_c.header_list = curl_slist_append(http_c.header_list, "User-Agent: cmp");
    if (!http_c.header_list) return -1;
    http_c.header_list = curl_slist_append(http_c.header_list, "Content-Type: text/xml");
    if (!http_c.header_list) return -1;
# ifdef ACS_FUSION
    char *expect_header = "Expect:";
    http_c.header_list = curl_slist_append(http_c.header_list, expect_header);
    if (!http_c.header_list) return -1;
# endif /* ACS_FUSION */
    curl = curl_easy_init();
    if (!curl) return -1;

#endif /* HTTP_CURL */

#ifdef HTTP_ZSTREAM
    http_c.stream = zstream_open(http_c.url, ZSTREAM_POST);
    if (!http_c.stream)
        return -1;

# ifdef ACS_HDM
    if (zstream_http_configure(http_c.stream, ZSTREAM_HTTP_COOKIES, 1))
# elif ACS_MULTI
    if (zstream_http_configure(http_c.stream, ZSTREAM_HTTP_COOKIES, 3))
# endif
        return -1;

    if (zstream_http_addheader(http_c.stream, "User-Agent", "cmp"))
        return -1;

    if (zstream_http_addheader(http_c.stream, "Content-Type", "text/xml"))
        return -1;
#endif /* HTTP_ZSTREAM */

    return 0;
}


int
http_send_message(struct cmp *cmp, char *msg_out, char **msg_in)
{
#ifdef HTTP_CURL
    CURLcode res;
    long http_code = 0;
    static char *ip_acs = NULL;
    char *ip = NULL;
    curl_easy_setopt(curl, CURLOPT_URL, http_c.url);
    curl_easy_setopt(curl, CURLOPT_USERNAME, cmp->conf.acs_userid);
    curl_easy_setopt(curl, CURLOPT_PASSWORD, cmp->conf.acs_passwd);
    curl_easy_setopt(curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC|CURLAUTH_DIGEST);
    curl_easy_setopt(curl, CURLOPT_HTTPHEADER, http_c.header_list);
    curl_easy_setopt(curl, CURLOPT_TIMEOUT, HTTP_TIMEOUT);
    curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, HTTP_TIMEOUT);
    curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
    curl_easy_setopt(curl, CURLOPT_COOKIEFILE, fc_cookies);
    curl_easy_setopt(curl, CURLOPT_COOKIEJAR, fc_cookies);

    curl_easy_setopt(curl, CURLOPT_POSTFIELDS, msg_out);
    if (msg_out)
        curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, (long) strlen(msg_out));
    else
        curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, 0);

    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, http_get_response);
    curl_easy_setopt(curl, CURLOPT_WRITEDATA, msg_in);

# ifdef DEVEL
    curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
# endif

    *msg_in = (char *) calloc (1, sizeof(char));

    res = curl_easy_perform(curl);

    if (!strlen(*msg_in))
        FREE(*msg_in);

    curl_easy_getinfo(curl, CURLINFO_PRIMARY_IP, &ip);
    if (ip) {
        if (!ip_acs || strcmp(ip_acs, ip) != 0) {
            FREE(ip_acs);
            ip_acs = strdup(ip);
            external_simple("allow_cr_ip", ip_acs);
        }
    }

    curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_code);

    if(http_code == 204)
    {
        cmp_LOG (INFO,"Receive HTTP 204 No Content");
    }

    if (http_code != 200 && http_code != 204)
        goto error;


    curl_easy_reset(curl);

    if (res) goto error;

#endif /* HTTP_CURL */

#ifdef HTTP_ZSTREAM
    char buffer[BUFSIZ];
    ssize_t rxed;
    if (zstream_reopen(http_c.stream, http_c.url, ZSTREAM_POST)) {
        /* something not good, let's try recreate */
        http_client_exit();
        if (http_client_init(cmp)) return -1;
    }


    if (msg_out) {
        zstream_write(http_c.stream, msg_out, strlen(msg_out));
    } else {
        zstream_write(http_c.stream, NULL , 0);
    }

    *msg_in = (char *) calloc (1, sizeof(char));
    while ((rxed = zstream_read(http_c.stream, buffer, sizeof(buffer))) > 0) {
        *msg_in = (char *) realloc(*msg_in, (strlen(*msg_in) + rxed + 1) * sizeof(char));
        if (!(*msg_in)) return -1;
        bzero(*msg_in + strlen(*msg_in), rxed + 1);
        memcpy(*msg_in + strlen(*msg_in), buffer, rxed);
    }

    /* we got no response, that is ok and defined in documentation */
    if (!strlen(*msg_in)) {
        FREE(*msg_in);
    }

    if (rxed < 0)
        goto error;

#endif /* HTTP_ZSTREAM */


    return 0;

error:
    FREE(*msg_in);
    return -1;
}

设置cookie选项,但http客户端不从服务器的http头部获取cookie id,因此客户端和服务器之间的通信将被破坏

感谢任何帮助

0 个答案:

没有答案