ext/revdispatch/libdispatch-0.1/src/ev_http.cc in evdispatch-0.3.0 vs ext/revdispatch/libdispatch-0.1/src/ev_http.cc in evdispatch-0.3.1

- old
+ new

@@ -285,10 +285,14 @@ m_response(new HttpResponse(url,fd)), m_handle(curl_easy_init()), m_client(dispatch.getHttpClient()) { assert(m_client); + // prevent libcurl from sending signals or installing signal handles + curl_easy_setopt(m_handle,CURLOPT_NOSIGNAL,1); + // we won't be sending progress + curl_easy_setopt(m_handle,CURLOPT_NOPROGRESS,1); init_curl(); } HttpRequest::HttpRequest( Dispatch &dispatch, const std::string &url ) : Request( 0, url ), @@ -344,10 +348,32 @@ { Request::set_key(key); m_response->id = key; } +void HttpRequest::set_opt( const std::string &key, struct curl_slist *slist ) +{ + Request::set_opt(key,slist); + std::map<std::string,CURLoption> key_loopup; + key_loopup["headers"] = CURLOPT_HTTPHEADER; + std::map<std::string,CURLoption>::iterator loc = key_loopup.find(key); + if( loc != key_loopup.end() ) { + CURLoption val_type = loc->second; + if( key == "headers" ) { + // construct a slist of headers + curl_easy_setopt( m_handle, val_type, slist ); + if( slist ) { + // we need to keep a reference to the options slist so we can cleanup after the request completes + this->m_response->add_to_free_list( slist ); + } + } + } + else { + printf("invalid option: %s", key.c_str() ); + } +} + void HttpRequest::set_opt( const std::string &key, const std::string &value ) { Request::set_opt(key,value); // convert the key into a curl value // #define CURLOPTTYPE_LONG 0 @@ -365,11 +391,11 @@ key_loopup["useragent"] = CURLOPT_USERAGENT; key_loopup["cookie"] = CURLOPT_COOKIE; key_loopup["post"] = CURLOPT_POSTFIELDS; std::map<std::string,CURLoption>::iterator loc = key_loopup.find(key); - if( loc != key_loopup.end() ){ + if( loc != key_loopup.end() ) { CURLoption val_type = loc->second; if( val_type >= CURLOPTTYPE_LONG && val_type < CURLOPTTYPE_OBJECTPOINT ) { long val = atoi(value.c_str()); curl_easy_setopt( m_handle, val_type, val ); } @@ -389,18 +415,29 @@ else { curl_easy_setopt( m_handle, val_type, value.c_str() ); } } } + else { + printf("invalid option: %s", key.c_str() ); + } } HttpResponse::HttpResponse( const std::string &url ) : Response(url), m_fd(-1) { } HttpResponse::HttpResponse( const std::string &url, int fd ) : Response(url), m_fd(fd) { +} +HttpResponse::~HttpResponse() +{ + for( std::set<struct curl_slist*>::iterator it = m_request_opts.begin(); + it != m_request_opts.end(); ++it ) { + curl_slist_free_all( *it ); + } + m_request_opts.clear(); } void HttpResponse::write( void *ptr, size_t realsize, size_t size, size_t nmemb ) { // printf( " write: %s, %d\n", this->name.c_str(), realsize ); if( m_fd == -1 ) {