加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
workerpub.c 4.18 KB
一键复制 编辑 原始数据 按行查看 历史
hotmocha 提交于 2015-04-02 22:29 . spider init
#include "workerpub.h"
void SetTaskName(struct WorkerEnv* env, char *strTaskName)
{
if (!env||!strTaskName)
return;
strcpy(env->workername, strTaskName);
}
/* header信息以\r\n分割 */
int SetHeader(struct HttpReq *req, char *header)
{
char *p = NULL;
CURLcode ret = CURLE_OK ;
if (req == NULL)
return SPIDER_ARG_ERR;
CURL *curl = req->curl;
if (curl == NULL ) return SPIDER_ARG_ERR;
p = strtok( header , "\r\n" ) ;
curl_slist_free_all(req->headerlist);
req->headerlist = NULL;
while( p )
{
req->headerlist = curl_slist_append( req->headerlist , p ) ;
p = strtok( NULL , "\r\n" ) ;
}
ret = curl_easy_setopt( curl , CURLOPT_HTTPHEADER , req->headerlist );
if (ret != CURLE_OK) {
return SPIDER_INTERNAL_ERR;
}
return 0;
}
int SetCookieFromFile(struct HttpReq *req, char *file)
{
CURL *curl = NULL;
int r = 0;
char *p = NULL;
CURLcode ret = CURLE_OK ;
if (req == NULL)
{
ErrorLog(__FILE__, __LINE__, "req is null");
return SPIDER_ARG_ERR;
}
curl = req->curl;
if (curl == NULL) return SPIDER_ARG_ERR;
if (file == NULL ) return 0;
r = access(file, F_OK | R_OK);
if (r)
{
ErrorLog(__FILE__, __LINE__, "file[%s] has no perm", file);
return SPIDER_FILE_ERR;
}
ret = curl_easy_setopt( curl , CURLOPT_COOKIEFILE, file );
if (ret != CURLE_OK) {
ErrorLog(__FILE__, __LINE__, "set cookiefile opt to curl failed");
return SPIDER_INTERNAL_ERR;
}
return 0;
}
int SetCookie(struct HttpReq *req, char *cookie)
{
CURLcode ret = CURLE_OK;
if (req == NULL)
return SPIDER_ARG_ERR;
if (req == NULL) {
ErrorLog(__FILE__, __LINE__, "arg is null");
return SPIDER_ARG_ERR;
}
CURL *curl = req->curl;
if (curl == NULL) {
ErrorLog(__FILE__, __LINE__, "curl is null");
return SPIDER_ARG_ERR;
}
ret = curl_easy_setopt(curl, CURLOPT_COOKIE, cookie);
if (ret != CURLE_OK) {
return SPIDER_INTERNAL_ERR;
}
return 0;
}
int SetPostdata(struct HttpReq *req, char *postdata)
{
CURLcode ret = CURLE_OK;
if (req == NULL)
return SPIDER_ARG_ERR;
if (req == NULL) {
ErrorLog(__FILE__, __LINE__, "arg is null");
return SPIDER_ARG_ERR;
}
CURL *curl = req->curl;
if (curl == NULL) {
ErrorLog(__FILE__, __LINE__, "curl is null");
return SPIDER_ARG_ERR;
}
ret = curl_easy_setopt(curl, CURLOPT_POSTFIELDS, postdata);
if (ret != CURLE_OK) {
return SPIDER_INTERNAL_ERR;
}
return 0;
}
int SetUrl(struct HttpReq *req, char *url)
{
CURL *curl = NULL;
int r = 0;
CURLcode ret = CURLE_OK;
if (req == NULL) {
ErrorLog(__FILE__, __LINE__, "arg is null");
return SPIDER_ARG_ERR;
}
curl = req->curl;
if (curl == NULL) {
ErrorLog(__FILE__, __LINE__, "curl is null");
return SPIDER_ARG_ERR;
}
ret = curl_easy_setopt(curl, CURLOPT_URL, url);
if (ret != CURLE_OK) {
return SPIDER_INTERNAL_ERR;
}
return 0;
}
void SetVerbose(struct WorkerEnv *env, int e)
{
if (!env) return;
env->verbose = e;
}
void SetHttpReqInputData(struct HttpReq *req, void *inputdata, void (*freeFunc)(void *))
{
req->inputdata = inputdata;
if (freeFunc)
req->FreeInputData = freeFunc;
else
req->FreeInputData = free;
}
int PrintRespHeader(struct HttpReq *req)
{
fprintf(stderr, "-----------req[%s]--header--------------\n", req->reqname);
fprintf(stderr, "%s\n", req->recvheader->base);
fprintf(stderr, "+++++++++++req[%s]++header++++++++++++++\n", req->reqname);
return 0;
}
int PrintRespBody(struct HttpReq *req)
{
fprintf(stderr, "-----------req[%s]--body--------------\n", req->reqname);
fprintf(stderr, "%s\n", req->recvbody->base);
fprintf(stderr, "+++++++++++req[%s]++body++++++++++++++\n", req->reqname);
return 0;
}
int SetCertFile(struct TaskEnv *tenv, struct HttpReq *req, char *filename)
{
CURLcode ret = CURLE_OK;
char curl_errbuf[1024];
char pathfile[512];
memset(curl_errbuf, 0x00, sizeof(curl_errbuf));
memset(pathfile, 0x00, sizeof(pathfile));
sprintf(pathfile, "%s/%s", tenv->basepath, filename);
curl_easy_setopt(req->curl, CURLOPT_ERRORBUFFER, curl_errbuf);
if (
((ret = curl_easy_setopt( req->curl , CURLOPT_SSL_VERIFYPEER , 1L )) == CURLE_OK) &&
((ret = curl_easy_setopt( req->curl , CURLOPT_CAINFO, pathfile)) == CURLE_OK) &&
((ret = curl_easy_setopt( req->curl , CURLOPT_SSL_VERIFYHOST , 2L )) == CURLE_OK)
)
{
return 0;
}
else {
return -1;
}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化