コード例 #1
0
// 登录
struct ResultStruct *login(char *url, char *usernamepassword)
{
    CURL *curl;
    CURLcode res;
    struct MemoryStruct *chunk = new struct MemoryStruct;
    curl_global_init(CURL_GLOBAL_ALL);
    curl = curl_easy_init();
    curl_slist *cookie;
    if(curl) {
        curl_easy_setopt(curl, CURLOPT_URL, url);
        curl_easy_setopt(curl, CURLOPT_POSTFIELDS, usernamepassword);//"user_name=hello&user_password=world"
        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
        curl_easy_setopt(curl, CURLOPT_WRITEDATA, chunk);
        curl_easy_setopt(curl, CURLOPT_COOKIEFILE, "");
        curl_easy_setopt(curl, CURLOPT_USERAGENT, "libcurl-agent/1.0");
        curl_easy_setopt(curl, CURLOPT_TIMEOUT, 5);
        res = curl_easy_perform(curl);
        cookie = get_cookies(curl);
        if(res != CURLE_OK)
            fprintf(stderr, "curl_easy_perform() failed: %s\n",curl_easy_strerror(res));
        curl_easy_cleanup(curl);
    }
    curl_global_cleanup();
    struct ResultStruct *rs = new struct ResultStruct;
    if (cookie) {
        rs->cookie_list = cookie->data;
        cookie = cookie->next;
        rs->session_id = cookie->data;
    }
    rs->memory = chunk->memory.c_str();
    return rs;
}
コード例 #2
0
ファイル: HttpResponse.cpp プロジェクト: anlebcoder/fibjs
result_t HttpResponse::addCookie(HttpCookie_base *cookie)
{
    obj_ptr<List_base> cookies;
    Variant v;

    v = cookie;
    get_cookies(cookies);
    cookies->push(v);

    return 0;
}
コード例 #3
0
ファイル: final.cpp プロジェクト: xiyouant/H3C_Auto_regist
void  run()
{
        CURL * curl;
        CURLcode res;
        int trycount = 0 ;
        
        curl_global_init(CURL_GLOBAL_ALL);
        /*初始话*/
        get_cookies(b,COOK);
        /*获取cookies*/
        post_id_key(c,ID,KEY,COOK);

        /* 发送密码和用户
        */
        while(1)
        {
                trycount++;
                if(trycount==300)
                {
                        make_a_heat(d);
                        /*10分钟一次心跳包*/
                        trycount = 0;
                }
                sleep(2);
                int cout=0;
                while(cheackInert(a))
                {
                        /*网络断开,重新登陆*/

                        time_t timep;
                        time(&timep);
                        fout<<ctime(&timep)<<"Intnet  can't conntecd "<<std::endl;

                        get_cookies(b,COOK);
                        post_id_key(c,ID,KEY,COOK);
                        cout++;

                        if(cout%10 == 0 )
                        {

                                time_t rawtime;
                                struct tm * timeinfo;

                                time(&rawtime);
                                timeinfo = localtime(&rawtime);

                                fout<<asctime(timeinfo)<<" "<<"多次登陆尝试失败"<<std::endl;
                        }

                        trycount = 0;// 重置闹钟
                }
                if( trycount%60 == 0 )
                {
                        time_t timep;
                        time(&timep);
                        fout<<ctime(&timep)<<"Intnet conntecd "<<std::endl;
                        curl_easy_cleanup(curl);
                }

        }
        curl_global_cleanup();


}
コード例 #4
0
ファイル: http.c プロジェクト: 309746069/FFmpeg
static int http_connect(URLContext *h, const char *path, const char *local_path,
                        const char *hoststr, const char *auth,
                        const char *proxyauth, int *new_location)
{
    HTTPContext *s = h->priv_data;
    int post, err;
    char headers[HTTP_HEADERS_SIZE] = "";
    char *authstr = NULL, *proxyauthstr = NULL;
    int64_t off = s->off;
    int len = 0;
    const char *method;
    int send_expect_100 = 0;

    /* send http header */
    post = h->flags & AVIO_FLAG_WRITE;

    if (s->post_data) {
        /* force POST method and disable chunked encoding when
         * custom HTTP post data is set */
        post            = 1;
        s->chunked_post = 0;
    }

    if (s->method)
        method = s->method;
    else
        method = post ? "POST" : "GET";

    authstr      = ff_http_auth_create_response(&s->auth_state, auth,
                                                local_path, method);
    proxyauthstr = ff_http_auth_create_response(&s->proxy_auth_state, proxyauth,
                                                local_path, method);
    if (post && !s->post_data) {
        send_expect_100 = s->send_expect_100;
        /* The user has supplied authentication but we don't know the auth type,
         * send Expect: 100-continue to get the 401 response including the
         * WWW-Authenticate header, or an 100 continue if no auth actually
         * is needed. */
        if (auth && *auth &&
            s->auth_state.auth_type == HTTP_AUTH_NONE &&
            s->http_code != 401)
            send_expect_100 = 1;
    }

    /* set default headers if needed */
    if (!has_header(s->headers, "\r\nUser-Agent: "))
        len += av_strlcatf(headers + len, sizeof(headers) - len,
                           "User-Agent: %s\r\n", s->user_agent);
    if (!has_header(s->headers, "\r\nAccept: "))
        len += av_strlcpy(headers + len, "Accept: */*\r\n",
                          sizeof(headers) - len);
    // Note: we send this on purpose even when s->off is 0 when we're probing,
    // since it allows us to detect more reliably if a (non-conforming)
    // server supports seeking by analysing the reply headers.
    if (!has_header(s->headers, "\r\nRange: ") && !post && (s->off > 0 || s->end_off || s->seekable == -1)) {
        len += av_strlcatf(headers + len, sizeof(headers) - len,
                           "Range: bytes=%"PRId64"-", s->off);
        if (s->end_off)
            len += av_strlcatf(headers + len, sizeof(headers) - len,
                               "%"PRId64, s->end_off - 1);
        len += av_strlcpy(headers + len, "\r\n",
                          sizeof(headers) - len);
    }
    if (send_expect_100 && !has_header(s->headers, "\r\nExpect: "))
        len += av_strlcatf(headers + len, sizeof(headers) - len,
                           "Expect: 100-continue\r\n");

    if (!has_header(s->headers, "\r\nConnection: ")) {
        if (s->multiple_requests)
            len += av_strlcpy(headers + len, "Connection: keep-alive\r\n",
                              sizeof(headers) - len);
        else
            len += av_strlcpy(headers + len, "Connection: close\r\n",
                              sizeof(headers) - len);
    }

    if (!has_header(s->headers, "\r\nHost: "))
        len += av_strlcatf(headers + len, sizeof(headers) - len,
                           "Host: %s\r\n", hoststr);
    if (!has_header(s->headers, "\r\nContent-Length: ") && s->post_data)
        len += av_strlcatf(headers + len, sizeof(headers) - len,
                           "Content-Length: %d\r\n", s->post_datalen);

    if (!has_header(s->headers, "\r\nContent-Type: ") && s->content_type)
        len += av_strlcatf(headers + len, sizeof(headers) - len,
                           "Content-Type: %s\r\n", s->content_type);
    if (!has_header(s->headers, "\r\nCookie: ") && s->cookies) {
        char *cookies = NULL;
        if (!get_cookies(s, &cookies, path, hoststr) && cookies) {
            len += av_strlcatf(headers + len, sizeof(headers) - len,
                               "Cookie: %s\r\n", cookies);
            av_free(cookies);
        }
    }
    if (!has_header(s->headers, "\r\nIcy-MetaData: ") && s->icy)
        len += av_strlcatf(headers + len, sizeof(headers) - len,
                           "Icy-MetaData: %d\r\n", 1);

    /* now add in custom headers */
    if (s->headers)
        av_strlcpy(headers + len, s->headers, sizeof(headers) - len);

    snprintf(s->buffer, sizeof(s->buffer),
             "%s %s HTTP/1.1\r\n"
             "%s"
             "%s"
             "%s"
             "%s%s"
             "\r\n",
             method,
             path,
             post && s->chunked_post ? "Transfer-Encoding: chunked\r\n" : "",
             headers,
             authstr ? authstr : "",
             proxyauthstr ? "Proxy-" : "", proxyauthstr ? proxyauthstr : "");

    av_log(h, AV_LOG_DEBUG, "request: %s\n", s->buffer);

    if ((err = ffurl_write(s->hd, s->buffer, strlen(s->buffer))) < 0)
        goto done;

    if (s->post_data)
        if ((err = ffurl_write(s->hd, s->post_data, s->post_datalen)) < 0)
            goto done;

    /* init input buffer */
    s->buf_ptr          = s->buffer;
    s->buf_end          = s->buffer;
    s->line_count       = 0;
    s->off              = 0;
    s->icy_data_read    = 0;
    s->filesize         = -1;
    s->willclose        = 0;
    s->end_chunked_post = 0;
    s->end_header       = 0;
    if (post && !s->post_data && !send_expect_100) {
        /* Pretend that it did work. We didn't read any header yet, since
         * we've still to send the POST data, but the code calling this
         * function will check http_code after we return. */
        s->http_code = 200;
        err = 0;
        goto done;
    }

    /* wait for header */
    err = http_read_header(h, new_location);
    if (err < 0)
        goto done;

    if (*new_location)
        s->off = off;

    err = (off == s->off) ? 0 : -1;
done:
    av_freep(&authstr);
    av_freep(&proxyauthstr);
    return err;
}
コード例 #5
0
static void readconfig() {
	const char	*val = NULL;
#ifdef DEBUG
	fprintf(stderr, "Reading configuration file...");
#endif

	config_t cfg;

	config_init(&cfg);
	if (!config_read_file(&cfg, config_file))   {
		char *message = "Config file not found in /etc/libnss.conf.\n";
		syslog(LOG_ERR, "%s", message);

#ifdef DEBUG
		fprintf(stderr, "%s", message);
		fprintf(stderr, "%s\n", config_error_text(&cfg));
#endif

		config_destroy(&cfg);  
		return;
	}

	readconfig_value(&cfg, "url_passwd", &url_passwd);
	readconfig_value(&cfg, "url_group", &url_group);
	readconfig_value(&cfg, "cafile", &cafile);
	readconfig_value(&cfg, "sslcheck", &sslcheck);
	readconfig_value(&cfg, "username", &username);
	readconfig_value(&cfg, "password", &password);

	if (get_cookies() == NULL) {
		int		i = 0;
		regex_t	envVar;
		readconfig_value(&cfg, "cookie_num", &val);
		if (val == NULL) goto end_config;
		int num_cookies = atoi(val);

		if(regcomp(&envVar, "\\$\\{[a-zA-Z0-9_-]+\\}", REG_EXTENDED))
			goto end_config;

		for(i=1; i <= num_cookies; ++i) {
			char 	strkey[512],
				*cookie_name = NULL,
				*cookie_value = NULL;

			sprintf(strkey, "cookie_%d_name", i);
			readconfig_value(&cfg, strkey, (const char **)&cookie_name);
			sprintf(strkey, "cookie_%d_value", i);
			readconfig_value(&cfg, strkey, (const char **)&cookie_value);
			if(!cookie_value || !cookie_name)
				goto on_gen_err;
			/* expand cookie_name */
			char		*tmpVal = cookie_name,
					tmpBuf[2048];
			tmpBuf[0] = '\0';
			regmatch_t	match; 
			while(!regexec(&envVar, tmpVal, 1, &match, 0)) {
				if(-1 == match.rm_so)
					break;
				strncat(tmpBuf, tmpVal, match.rm_so);
				char            *pEnd = &tmpVal[match.rm_eo-1],
						tVal = *pEnd;
				*pEnd = '\0';
				const char	*envVal = getenv(tmpVal + match.rm_so + 2);
				*pEnd = tVal;
				tmpVal += match.rm_eo;
				if(envVal) strcat(tmpBuf, envVal);
			}
			free(cookie_name);
			cookie_name = strdup(tmpBuf);
			if(!cookie_name) goto on_gen_err;
			/* do the same for cookie_value */
			tmpVal = cookie_value;
			tmpBuf[0] = '\0';
			while(!regexec(&envVar, tmpVal, 1, &match, 0)) {
				if(-1 == match.rm_so)
					break;
				strncat(tmpBuf, tmpVal, match.rm_so);
				char            *pEnd = &tmpVal[match.rm_eo-1],
						tVal = *pEnd;
				*pEnd = '\0';
				const char	*envVal = getenv(tmpVal + match.rm_so + 2);
				*pEnd = tVal;
				tmpVal += match.rm_eo;
				if(envVal) strcat(tmpBuf, envVal);
			}
			free(cookie_value);
			cookie_value = strdup(tmpBuf);
			if(!cookie_value) goto on_gen_err;
			/* add to cookies */
			BODY *curcookie = (BODY *) malloc(sizeof(BODY));
			if(!curcookie) goto on_gen_err;
			curcookie->row = (char *) malloc(strlen(cookie_name)+strlen(cookie_value)+2);
			if(!curcookie->row) {
				free(curcookie);
				goto on_gen_err;
			}
			sprintf(curcookie->row, "%s=%s", cookie_name, cookie_value);
			curcookie->next = get_cookies();
			set_cookies(curcookie);

#ifdef DEBUG
			fprintf(stderr, "Read new cookie: [%s] => %s\n", cookie_name, cookie_value);
#endif

on_gen_err:
			if(cookie_value) free(cookie_value);
			if(cookie_name) free(cookie_name);
		}
		regfree(&envVar);
	}

end_config:
	config_destroy(&cfg);
	if(val) free((void*)val); 
}