Exemplo n.º 1
0
int
ocrc_process(OCstate* state)
{
    int stat = 0;
    char* value = NULL;
    OCURI* uri = state->uri;
    char* url_userpwd = NULL;
    char* url_hostport = NULL;

    if(!ocglobalstate.initialized)
	ocinternalinitialize();
    if(!ocglobalstate.rc.loaded)
	ocrc_load();
    /* Note, we still must do this function even if
       ocglobalstate.rc.ignore is set in order
       to getinfo e.g. user:pwd from url
    */

    url_userpwd = uri->userpwd;
    url_hostport = occombinehostport(uri);
    if(url_hostport == NULL)
	return OC_ENOMEM;

    value = ocrc_lookup("HTTP.DEFLATE",url_hostport);
    if(value != NULL) {
        if(atoi(value)) state->curlflags.compress = 1;
        if(ocdebug > 0)
            oclog(OCLOGNOTE,"HTTP.DEFLATE: %ld", state->curlflags.compress);
    }
    if((value = ocrc_lookup("HTTP.VERBOSE",url_hostport)) != NULL) {
        if(atoi(value)) state->curlflags.verbose = 1;
        if(ocdebug > 0)
            oclog(OCLOGNOTE,"HTTP.VERBOSE: %ld", state->curlflags.verbose);
    }
    if((value = ocrc_lookup("HTTP.TIMEOUT",url_hostport)) != NULL) {
        if(atoi(value)) state->curlflags.timeout = atoi(value);
        if(ocdebug > 0)
            oclog(OCLOGNOTE,"HTTP.TIMEOUT: %ld", state->curlflags.timeout);
    }
    if((value = ocrc_lookup("HTTP.USERAGENT",url_hostport)) != NULL) {
        if(atoi(value)) state->curlflags.useragent = strdup(value);
        if(state->curlflags.useragent == NULL) {stat = OC_ENOMEM; goto done;}
        if(ocdebug > 0)
            oclog(OCLOGNOTE,"HTTP.USERAGENT: %s", state->curlflags.useragent);
    }

    if(
          (value = ocrc_lookup("HTTP.COOKIEFILE",url_hostport))
       || (value = ocrc_lookup("HTTP.COOKIE_FILE",url_hostport))
       || (value = ocrc_lookup("HTTP.COOKIEJAR",url_hostport))
       || (value = ocrc_lookup("HTTP.COOKIE_JAR",url_hostport))
      ) {
        state->curlflags.cookiejar = strdup(value);
        if(state->curlflags.cookiejar == NULL) {stat = OC_ENOMEM; goto done;}
        if(ocdebug > 0)
            oclog(OCLOGNOTE,"HTTP.COOKIEJAR: %s", state->curlflags.cookiejar);
    }

    if((value = ocrc_lookup("HTTP.PROXY_SERVER",url_hostport)) != NULL) {
        stat = ocparseproxy(state,value);
        if(stat != OC_NOERR) goto done;
        if(ocdebug > 0)
            oclog(OCLOGNOTE,"HTTP.PROXY_SERVER: %s", value);
    }

    if((value = ocrc_lookup("HTTP.SSL.VALIDATE",url_hostport)) != NULL) {
        if(atoi(value)) {
	    state->ssl.verifypeer = 1;
	    state->ssl.verifyhost = 1;
            if(ocdebug > 0)
                oclog(OCLOGNOTE,"HTTP.SSL.VALIDATE: %ld", 1);
	}
    }

    if((value = ocrc_lookup("HTTP.SSL.CERTIFICATE",url_hostport)) != NULL) {
        state->ssl.certificate = strdup(value);
        if(state->ssl.certificate == NULL) {stat = OC_ENOMEM; goto done;}
        if(ocdebug > 0)
            oclog(OCLOGNOTE,"HTTP.SSL.CERTIFICATE: %s", state->ssl.certificate);
    }

    if((value = ocrc_lookup("HTTP.SSL.KEY",url_hostport)) != NULL) {
        state->ssl.key = strdup(value);
        if(state->ssl.key == NULL) {stat = OC_ENOMEM; goto done;}
        if(ocdebug > 0)
            oclog(OCLOGNOTE,"HTTP.SSL.KEY: %s", state->ssl.key);
    }

    if((value = ocrc_lookup("HTTP.SSL.KEYPASSWORD",url_hostport)) != NULL) {
        state->ssl.keypasswd = strdup(value);
        if(state->ssl.keypasswd == NULL) {stat = OC_ENOMEM; goto done;}
        if(ocdebug > 0)
            oclog(OCLOGNOTE,"HTTP.SSL.KEYPASSWORD: %s", state->ssl.keypasswd);
    }

    if((value = ocrc_lookup("HTTP.SSL.CAINFO",url_hostport)) != NULL) {
        state->ssl.cainfo = strdup(value);
        if(state->ssl.cainfo == NULL) {stat = OC_ENOMEM; goto done;}
        if(ocdebug > 0)
            oclog(OCLOGNOTE,"HTTP.SSL.CAINFO: %s", state->ssl.cainfo);
    }

    if((value = ocrc_lookup("HTTP.SSL.CAPATH",url_hostport)) != NULL) {
        state->ssl.capath = strdup(value);
        if(state->ssl.capath == NULL) {stat = OC_ENOMEM; goto done;}
        if(ocdebug > 0)
            oclog(OCLOGNOTE,"HTTP.SSL.CAPATH: %s", state->ssl.capath);
    }

    if((value = ocrc_lookup("HTTP.SSL.VERIFYPEER",url_hostport)) != NULL) {
        char* s = strdup(value);
        int tf = 0;
        if(s == NULL || strcmp(s,"0")==0 || strcasecmp(s,"false")==0)
            tf = 0;
        else if(strcmp(s,"1")==0 || strcasecmp(s,"true")==0)
            tf = 1;
        else
            tf = 1; /* default if not null */
        state->ssl.verifypeer = tf;
        if(ocdebug > 0)
            oclog(OCLOGNOTE,"HTTP.SSL.VERIFYPEER: %d", state->ssl.verifypeer);
	free(s);
    }

    if((value = ocrc_lookup("HTTP.NETRC",url_hostport)) != NULL) {
        if(state->curlflags.netrc != NULL)
	    free(state->curlflags.netrc);
        state->curlflags.netrc = strdup(value);
        if(state->curlflags.netrc == NULL) {stat = OC_ENOMEM; goto done;}
        if(ocdebug > 0)
            oclog(OCLOGNOTE,"HTTP.NETRC: %s", state->curlflags.netrc);
    }

    { /* Handle various cases for user + password */
	/* First, see if the user+pwd was in the original url */
	char* userpwd = NULL;
	char* user = NULL;
	char* pwd = NULL;
	if(url_userpwd != NULL)
	    userpwd = url_userpwd;
	else {
   	    user = ocrc_lookup("HTTP.CREDENTIALS.USER",url_hostport);
	    pwd = ocrc_lookup("HTTP.CREDENTIALS.PASSWORD",url_hostport);
	    userpwd = ocrc_lookup("HTTP.CREDENTIALS.USERPASSWORD",url_hostport);
	}
	if(userpwd == NULL && user != NULL && pwd != NULL) {
	    userpwd = combinecredentials(user,pwd);
	    state->creds.userpwd = userpwd;
	} else if(userpwd != NULL)
	    state->creds.userpwd = strdup(userpwd);
    }

done:
    if(url_hostport != NULL) free(url_hostport);
    return stat;
}
Exemplo n.º 2
0
/*
"Inverse" of ocset_curlflag;
Given a flag and value, it updates state.
Update a specific flag from state->curlflags.
*/
OCerror
ocset_curlstate(OCstate* state, int flag, void* value)
{
    OCerror stat = OC_NOERR;

    switch (flag) {

    case CURLOPT_USERPWD:
        if(state->creds.userpwd != NULL) free(state->creds.userpwd);
	state->creds.userpwd = strdup((char*)value);
	break;

    case CURLOPT_COOKIEJAR: case CURLOPT_COOKIEFILE:
        if(state->curlflags.cookiejar != NULL) free(state->curlflags.cookiejar);
	state->curlflags.cookiejar = strdup((char*)value);
	break;

    case CURLOPT_NETRC: case CURLOPT_NETRC_FILE:
        if(state->curlflags.netrc != NULL) free(state->curlflags.netrc);
	state->curlflags.netrc = strdup((char*)value);
	break;

    case CURLOPT_VERBOSE:
	state->curlflags.verbose = (long)value;
	break;

    case CURLOPT_TIMEOUT:
	state->curlflags.timeout = (long)value;
	break;

    case CURLOPT_USERAGENT:
        if(state->curlflags.useragent != NULL) free(state->curlflags.useragent);
        state->curlflags.useragent = strdup((char*)value);
	break;

    case CURLOPT_FOLLOWLOCATION:
	/* no need to store; will always be set */
	break;

    case CURLOPT_MAXREDIRS:
	/* no need to store; will always be set */
	break;

    case CURLOPT_ERRORBUFFER:
	/* no need to store; will always be set */
	break;

    case CURLOPT_ENCODING:
	/* no need to store; will always be set to fixed value */
	break;

    case CURLOPT_PROXY:
	/* We assume that the value is the proxy url */
	if(state->proxy.host != NULL) free(state->proxy.host);
	if(state->proxy.userpwd != NULL) free(state->proxy.userpwd);
	state->proxy.host = NULL;
	state->proxy.userpwd = NULL;
	if(!ocparseproxy(state,(char*)value))
		{stat = OC_EINVAL; goto done;}
	break;

    case CURLOPT_SSLCERT:
	if(state->ssl.certificate != NULL) free(state->ssl.certificate);
	state->ssl.certificate = strdup((char*)value);
	break;
    case CURLOPT_SSLKEY:
	if(state->ssl.key != NULL) free(state->ssl.key);
	state->ssl.key = strdup((char*)value);
	break;
    case CURLOPT_KEYPASSWD:
	if(state->ssl.keypasswd!= NULL) free(state->ssl.keypasswd);
	state->ssl.keypasswd = strdup((char*)value);
	break;
    case CURLOPT_SSL_VERIFYPEER:
      state->ssl.verifypeer = (long)value;
      break;
    case CURLOPT_SSL_VERIFYHOST:
      state->ssl.verifyhost = (long)value;
      break;
    case CURLOPT_CAINFO:
      if(state->ssl.cainfo != NULL) free(state->ssl.cainfo);
      state->ssl.cainfo = strdup((char*)value);
      break;
    case CURLOPT_CAPATH:
	if(state->ssl.capath != NULL) free(state->ssl.capath);
	state->ssl.capath = strdup((char*)value);
	break;

    default: {
	struct OCCURLFLAG* f = occurlflagbyflag(flag);
	if(f != NULL)
	    oclog(OCLOGWARN,"Attempt to add unexpected curl flag to state: %s",
				f->name);
	} break;
    }
done:
    return stat;
}