cURL / Mailing Lists / curl-users / Single Mail

curl-users

CURLOPT_PRIVATE returning incorrect values for delayed documents : Possible bug

From: Sharad Kumar Singh <SHARAD.KUMAR.SINGH_at_saic.com>
Date: Tue, 11 Nov 2008 16:58:18 +0530

Hi All,

I am using libcurl 7.18.2 on fedora. I am using the following program to
download 3 files http://docserver/OFP181.vxml ,
http://139.121.249.198:8080/ir/vxml-srgs-ecmascript/608/cgi-bin/608-sleep8sec-tmp.jsp and http://139.121.249.198:8080/ir/vxml-srgs-ecmascript/608/cgi-bin/608-sleep8sec-tmp.jsp .

The last two files are jsp with sleep time of 8 second,i.e. they will be
downloaded after 8 seconds. And I am setting CURLOPT_PRIVATE as 1 ,2 ,3
for the respective files. The problem is when I receive CURLINFO_PRIVATE
it is not 1 , 2, 3 but 2 , 3, 3. So i think there must be something in
the libcurl code that causes this.

Also instead of sId as char array i give it as a hard coded string "1"
"2" and "3" directly to the api , the result is fine.

I am also including the code that i am using and the files that i am
downloading.

------------------------------------c code-----------------------------

#include <stdio.h>
#include <string.h>

/* somewhat unix-specific */
#include <sys/time.h>
#include <unistd.h>

/* curl stuff */
#include <curl/curl.h>
size_t WriteMemoryCallback(void *ptr, size_t size, size_t nmemb, void
*data);
struct Memory
{
  char *memory;
  size_t size;
};
typedef struct Memory MemoryStruct;
/*
 * * Simply download two HTTP files!
 * */
int main(int argc, char **argv)
{
  CURL *http_handle;
  CURL *http_handle2;
  CURL *http_handle3;
  CURLM *multi_handle;
  int Q;
  int r_status;
  int still_running; /* keep number of running handles */
   CURLMsg* msg = NULL;
  http_handle = curl_easy_init();
  http_handle2 = curl_easy_init();
  http_handle3 = curl_easy_init();

  /* set options */
  char s_Id[512]={'\0',};
// memset(s_Id,0,512);
  sprintf(s_Id,"%d",1);
  curl_easy_setopt(http_handle, CURLOPT_URL,
"http://docserver/OFP181.vxml");
  fprintf(stderr,"Using Curl %p \n",http_handle);
  fprintf(stderr,"Setting PRIVATE DATA %s\n\n",s_Id);
  curl_easy_setopt(http_handle, CURLOPT_PRIVATE, s_Id);
// curl_easy_setopt(http_handle, CURLOPT_PRIVATE, "1"); works fine
  curl_easy_setopt(http_handle, CURLOPT_VERBOSE, 0);

curl_easy_setopt(http_handle,CURLOPT_WRITEFUNCTION,WriteMemoryCallback);

  /* set options */
  curl_easy_setopt(http_handle2, CURLOPT_URL,
"http://139.121.249.198:8080/ir/vxml-srgs-ecmascript/608/cgi-bin/608-sleep8sec-tmp.jsp");
// memset(s_Id,0,512);
  sprintf(s_Id,"%d",2);
  fprintf(stderr,"Using Curl %p \n",http_handle2);
  fprintf(stderr,"Setting PRIVATE DATA %s\n\n",s_Id);
  curl_easy_setopt(http_handle2, CURLOPT_PRIVATE, s_Id);
// curl_easy_setopt(http_handle2,CURLOPT_TIMEOUT,5);
  curl_easy_setopt(http_handle2,
CURLOPT_POSTFIELDS,"sessionid=83f699bc0000011d25189e720a68a3b0");
  curl_easy_setopt(http_handle2,
CURLOPT_POSTFIELDSIZE,strlen("sessionid=83f699bc0000011d25189e720a68a3b0"));
  curl_easy_setopt(http_handle2, CURLOPT_POST, 1);
  curl_easy_setopt(http_handle2, CURLOPT_VERBOSE, 0);

curl_easy_setopt(http_handle2,CURLOPT_WRITEFUNCTION,WriteMemoryCallback);

  /* set options */
  curl_easy_setopt(http_handle3, CURLOPT_URL,
"http://139.121.249.198:8080/ir/vxml-srgs-ecmascript/608/cgi-bin/608-sleep8sec-tmp.jsp");
// memset(s_Id,0,512);
  sprintf(s_Id,"%d",3);
  fprintf(stderr,"Using Curl %p \n",http_handle3);
  fprintf(stderr,"Setting PRIVATE DATA %s\n\n",s_Id);
  curl_easy_setopt(http_handle3, CURLOPT_PRIVATE, s_Id);
  curl_easy_setopt(http_handle3, CURLOPT_VERBOSE, 0);

curl_easy_setopt(http_handle3,CURLOPT_WRITEFUNCTION,WriteMemoryCallback);

/* init a multi stack */
  multi_handle = curl_multi_init();

  /* add the individual transfers */
  curl_multi_add_handle(multi_handle, http_handle);
  curl_multi_add_handle(multi_handle, http_handle2);
  curl_multi_add_handle(multi_handle, http_handle3);
  int mInter_handles=3;
  int numHandles=0;
  /* we start some action by calling perform right away */
  while(1)
    {
        while(CURLM_CALL_MULTI_PERFORM ==
curl_multi_perform(multi_handle, &numHandles))
        {
            if(mInter_handles - numHandles > 0 )
            {
                break;
            }
        }
        while((msg = curl_multi_info_read(multi_handle, &Q)))
        {
            if (NULL != msg && msg->msg == CURLMSG_DONE)
            {
                char *s_Id =NULL;
                char *url =NULL;
                CURL *pEasyHandle = msg->easy_handle ;
                int i_SID=0;
                if(NULL != pEasyHandle)
                {
                    curl_easy_getinfo(pEasyHandle, CURLINFO_PRIVATE,
&s_Id);
                    curl_easy_getinfo(pEasyHandle,
CURLINFO_EFFECTIVE_URL, &url);

curl_easy_getinfo(pEasyHandle,CURLINFO_RESPONSE_CODE,&r_status);
                    fprintf(stderr,"EASY ADDR %p\n",pEasyHandle);
                    fprintf(stderr,"PRIVATE DATA GOT %s\n",s_Id);
                    fprintf(stderr,"URL %s RESPONSE %d \n\n
\n",url,r_status);
                    if(strlen(s_Id) > 0)
                    {
                        i_SID=atoi(s_Id);
// free(s_Id);
                    }
                    s_Id=NULL;
                    curl_multi_remove_handle(multi_handle, pEasyHandle);
                    mInter_handles--;
                }
            }
        }
    }
                

  curl_multi_cleanup(multi_handle);

  curl_easy_cleanup(http_handle);
  curl_easy_cleanup(http_handle2);
  curl_easy_cleanup(http_handle3);
   
  return 0;
}

size_t WriteMemoryCallback(void *ptr, size_t size, size_t nmemb, void
*data)
{
    size_t realsize = size * nmemb;
    MemoryStruct *mem = (MemoryStruct *)data;
    /*if(NULL != mem)
    {
        mem->memory = (char *)myrealloc(mem->memory, mem->size +
realsize + 1);
    }
    if(NULL != mem && mem->memory)
    {
        memcpy_t(&(mem->memory[mem->size]),(mem->size + realsize +
1),ptr, realsize);
        mem->size += realsize;
        mem->memory[mem->size] = 0;
    }*/
// printf("read %d\n",realsize);
    return realsize;
}

---------------------------------------------------------
-------------http://docserver/OFP181.vxml-----------------
<vxml version="2.0" xmlns="http://www.w3.org/2001/vxml">
    <form>
        <block>
            <var name="terminate" expr="'Y'"/>
            <var name="disconnect" expr="'null'"/>
            <var name="sessionid"
expr="'83f699bc0000011d25189e720a68a3b0'"/>

            <prompt>This is the test initialization</prompt>
            <submit
next="http://139.121.249.198:8080/ir/vxml-srgs-ecmascript/608/cgi-bin/608-sleep8sec-tmp.jsp" method="post"
                    namelist="sessionid"/>
        </block>
        <catch event="error.noresource">
          <log> PlayAnncIVR error.noresource/></log>
          <disconnect/>
        </catch>

        <catch event="error.connection">
          <log> PlayAnncIVR error.connection/></log>
          <exit/>
        </catch>

    </form>
</vxml>
------------------------------------------
-------------http://139.121.249.198:8080/ir/vxml-srgs-ecmascript/608/cgi-bin/608-sleep8sec-tmp.jsp---------
<%@ page language="java" contentType="text/xml" %><%@ page
import="java.io.BufferedInputStream" %><%@ page
import="java.io.InputStreamReader" %><%@ page import="java.net.URL" %><%
@ page import="java.net.URLConnection" %><%@ page
import="java.io.BufferedReader" %><%@ page import="java.util.Date" %><%@
page import="com.oreilly.servlet.multipart.MultipartParser" %><%@ page
import="com.oreilly.servlet.multipart.Part" %><%!

    

    // Handles server side includes so they can be parsed

    private class JSPIncluder
    {
        void readInput(HttpServletRequest request, String
strIncludePath) throws JspException
        {
            URLConnection conn;
            // Get URL
            StringBuffer strUrl = request.getRequestURL();
            String strUri = strUrl.toString();
            int nFindSlash = strUri.lastIndexOf("/");
            if (nFindSlash != -1)
            {strUri = strUri.substring(0, nFindSlash + 1);}
            strUri += strIncludePath;
            // Open connection
            try{
                conn = (new URL(strUri)).openConnection();
               conn.setDoInput(true);
               conn.setDoOutput(false);
               conn.connect();
            }
            catch (Exception e){throw new JspException(e.toString());}
            try
            {
                BufferedReader in = new BufferedReader(new
InputStreamReader(conn.getInputStream()));
                StringBuffer buff = new StringBuffer();
                char[] chars = new char[2048];
                int nLen;
                while ((nLen = in.read(chars, 0, chars.length)) >= 0)
                {
                    buff.append(chars, 0, nLen);
                }
                m_strBuffer = buff.toString();
                in.close();
            }
            catch (Exception e)
            {
                throw new JspException(e.toString());
            }
        }
       boolean replace(String strFind, String strReplace)
        {
           boolean bFound = false;
            if (m_strBuffer != null && m_strBuffer.length() > 0)
            {
                int a = 0;
                int b = 0;
                while (true)
                { a = m_strBuffer.indexOf(strFind, b);
                  if (a != -1)
                   {
                       m_strBuffer = m_strBuffer.substring(0, a) +
strReplace + m_strBuffer.substring(a + strFind.length());
                       b = a + strReplace.length();
                       bFound = true;
                   }
                   else{break;}}}return bFound;}
        void doOutput(PageContext context) throws JspException
        {JspWriter out =
context.getOut();try{out.print(m_strBuffer.toString());}catch (Exception
e){throw new JspException(e.toString());} }
        private String m_strBuffer;
    }
   private class MultiPartHandler
    {
       HttpServletRequest request;
       private MultiPartHandler(HttpServletRequest req)
       { request = req;}
        boolean find(String strFind) throws JspException
        { MultipartParser parser;
            Part part;
           String strName;
           if((request.getContentType() !=
null)&&(request.getContentType().startsWith("multipart/form-data")))
           {
                try
                {
                    parser = new MultipartParser(request,
request.getContentLength());
                    while ((part = parser.readNextPart()) != null)
                    {
                        strName = part.getName();
                        if(strName.equals(strFind))
                        {
                            return true;
                        }
                    }
                }
                catch (Exception e)
                {
                    throw new JspException(e.toString());
                }
            }
            return false;
         }
     }
  private class Result {
    String dest;long sleep = 0;boolean expiresHeaderSet = false;long
expires = 0;boolean include = false;StringBuffer comments = new
StringBuffer();int statusCode = 200;}
  private final String NL = System.getProperty("line.separator");
  private void determineResult(HttpServletRequest request, Result
result, MultiPartHandler multipart) throws JspException
  {
      result.dest = "../608-next.vxml";result.include =
true;result.sleep = 8;return;
  }

%><%
    Result myResult = new Result();
    MultiPartHandler myMultiPart = new MultiPartHandler(request);
    determineResult(request, myResult, myMultiPart);
    response.setStatus(myResult.statusCode);
    if (myResult.sleep > 0)
    {
       try
        {Thread.sleep(myResult.sleep * 1000);}
        catch (InterruptedException e)
        { throw new JspException(e.toString()); }
    }
    if (myResult.expiresHeaderSet){ Date now = new Date();long nMillis =
now.getTime(); response.setDateHeader("Expires", nMillis +
myResult.expires*1000);}
    if (myResult.include) {
    %><?xml version="1.0" ?>

<vxml version="2.0" xmlns="http://www.w3.org/2001/vxml"><form>
    <block><% String comments = myResult.comments.toString();
   if (comments.length()>0) {%>
      <log>
          <![CDATA[<%= comments %>]]>
      </log><%}%>
      <prompt>Test is now complete</prompt>
    </block>
  </form>
</vxml>

<%}%>
-------------------------------------------------------

Thanks And Regards

-------------------------------------------------------------------
List admin: http://cool.haxx.se/cgi-bin/mailman/listinfo/curl-users
FAQ: http://curl.haxx.se/docs/faq.html
Etiquette: http://curl.haxx.se/mail/etiquette.html
Received on 2008-11-11