cURL / Mailing Lists / curl-library / Single Mail

curl-library

Help needed with CURLOPT_HEADERDATA and friends

From: John Doe <gb10hkzo-libcurl_at_yahoo.co.uk>
Date: Mon, 10 Sep 2012 18:43:03 +0100 (BST)

Hello list, I'm afraid I can't make head or tail of the sparse documentation, probably because I haven't been programming in C/C++ long enough to be used to such sparseness.  But I digress.  I'm trying to implement a cheap and nasty proof of concept implementation of an Amazon Glacier interface to enable me to test some stuff.  To save you reading the Amazon docs, on both successful and bad attempts, you get back an "x-amzn-RequestId" HTTP header.  Then on a good attempt, you get another HTTP header called "x-amzn-ArchiveID", and on a bad attempt, you get a JSON formatted error message in the body. The goal of my libcurl coolness is to pick this stuff up and stick it in an SQLITE db table for future parsing at leisure. So far, I've cobbled together the code below, it picks up x-amzn-Requestid ok...... but then I can't get it to deal with the JSON error and the program segfaults.   Not understanding libcurl much, I'm thinking its something do do with the fact that HTTP header and body are dealt with by seperate functions and so my class is getting terribly confused trying to find stuff in memory that doesn't exist.  Problem is (a) I'm not sure this is the correct interpretation of my problem, and (b) I've no idea where to start fixing it..... I've tried various CURLOPT additions/removals but that seems to have no effect. Thanks in advance for your help, keep up the great work on Curl and apologies for the poor quality of the code that follows...... P.S. Is the curl c++ wrapper project dead ?  I see it hasn't been touched since 2009 ?  Hence I've been just using good old libcurl straight from the horse's mouth. /***** ***** SNIPPET FROM THE MAIN ROUTINE ****/ void mynamespace::curlEasy::curlPostFile { mynamespace::HttpHeaders curlHeaderParser;    curl_easy_setopt(curlCtx, CURLOPT_HEADERDATA, &curlHeaderParser); } /***** ***** MY CALLBACK ****/ std::size_t mynamespace::curlEasy::curlHeaderCallback(void* data, std::size_t blockSize, std::size_t numBlocks, void* userPtr) {     std::size_t dataSz = blockSize * numBlocks;     char *d = (char*) data;     mynamespace::HttpHeaders *pHeaders = (mynamespace::HttpHeaders *)(userPtr);     int result = 0;     if (pHeaders != NULL) {         std::string s = "";         s.append(d, dataSz);         pHeaders->addHeader(s);     }     //     return dataSz; } /***** ***** MY CALLBACK FUNCTION ****/ void mynamespace::HttpHeaders::addHeader(std::string& s) {     std::string curlKey;     boost::trim(s);     std::stringstream curlSS(s);     std::string curlVal;  // HTTP/1.1     if (s.compare(0, 1, vJSONOpen, 0, 1) == 0) {         if (s.compare(vJSONEmpty) != 0) {             mynamespace::SQLite sqlDbx;             sqlDbx.logAmzGlacierError(sqlDB,amzRid,s);         }     } else if (s.compare(0, 8, vGoodHeader, 0, 8) == 0) {         uploadedOK = false;         if (s.compare(vGoodHeader) == 0) {             uploadedOK = true;             ++httpHeaderCount;         } else if (s.compare(v100Continue) != 0) {             uploadedOK = false;             b100Continue = true;             httpHeaderCount = 1;         } else {             uploadedOK = false;             ++httpHeaderCount;         }     } else {         if (s.size() > 2) {             //             if (getline(curlSS, curlKey, ':')) {                 getline(curlSS, curlVal);             }             boost::trim(curlKey);             boost::trim(curlVal);             if (curlKey.compare("x-amzn-RequestId") == 0) {                 mynamespace::SQLite sqlDbx;                 sqlDbx.logAmzGlacierNewRecord(sqlDB,curlVal,vFilName);                 amzRid = curlVal;             }             //             if (uploadedOK) {                 hdrVals.insert(std::pair<std::string, std::string > (curlKey, curlVal));                 if (verboseMode) {                     std::cout << "upload successful....key:" + curlKey << std::endl;                     std::cout << "upload successful....val:" + curlVal << std::endl;                 }             } else if (!uploadedOK) {                 hdrVals.insert(std::pair<std::string, std::string > (curlKey, curlVal));                 if (verboseMode) {                     std::cout << "upload failed....key:" + curlKey << std::endl;                     std::cout << "upload failed....val:" + curlVal << std::endl;                 }             }         }     } }

-------------------------------------------------------------------
List admin: http://cool.haxx.se/list/listinfo/curl-library
Etiquette: http://curl.haxx.se/mail/etiquette.html
Received on 2012-09-10