* add cURL read-callbackhook, so we can read HTTP answers into StrBufs
authorWilfried Göesgens <willi@citadel.org>
Sun, 5 Jul 2009 20:25:36 +0000 (20:25 +0000)
committerWilfried Göesgens <willi@citadel.org>
Sun, 5 Jul 2009 20:25:36 +0000 (20:25 +0000)
libcitadel/lib/libcitadel.h
libcitadel/lib/stringbuf.c

index bae0441c3a5ca3101cae01ee8a069a7779aa8be4..a2337e8a877bff2f772ff839787da30423a5af7a 100644 (file)
@@ -283,6 +283,7 @@ int StrBufExtractNext_int(const StrBuf* Source, const char **pStart, char separa
 
 void StrBufAppendBufPlain(StrBuf *Buf, const char *AppendBuf, long AppendSize, unsigned long Offset);
 void StrBufAppendBuf(StrBuf *Buf, const StrBuf *AppendBuf, unsigned long Offset);
+size_t CurlFillStrBuf_callback(void *ptr, size_t size, size_t nmemb, void *stream);
 void StrBufAppendPrintf(StrBuf *Buf, const char *format, ...);
 #ifdef SHOW_ME_VAPPEND_PRINTF
 /* so owe don't create an include depndency, this is just visible on demand. */
@@ -315,7 +316,8 @@ int StrBufSanitizeAscii(StrBuf *Buf, const char Mute);
 void StrBufUrlescAppend(StrBuf *OutBuf, const StrBuf *In, const char *PlainIn);
 long StrEscAppend(StrBuf *Target, const StrBuf *Source, const char *PlainIn, int nbsp, int nolinebreaks);
 long StrECMAEscAppend(StrBuf *Target, const StrBuf *Source, const char *PlainIn);
-void StrMsgEscAppend(StrBuf *Target, StrBuf *Source, const char *PlainIn);
+void StrMsgEscAppend(StrBuf *Target, const StrBuf *Source, const char *PlainIn);
+void StrIcalEscAppend(StrBuf *Target, const StrBuf *Source, const char *PlainIn);
 
 long StrTol(const StrBuf *Buf);
 int StrToi(const StrBuf *Buf);
index 8d665866ca05c478d2a14e064fc29822e3affff2..599b482c0d9b6ff0475fb4cd301429daeb1864e4 100644 (file)
@@ -564,6 +564,23 @@ void StrBufAppendBufPlain(StrBuf *Buf, const char *AppendBuf, long AppendSize, u
        Buf->buf[Buf->BufUsed] = '\0';
 }
 
+/**
+ * \brief Callback for cURL to append the webserver reply to a buffer
+ * params pre-defined by the cURL API; see man 3 curl for mre info
+ */
+size_t CurlFillStrBuf_callback(void *ptr, size_t size, size_t nmemb, void *stream)
+{
+
+       StrBuf *Target;
+
+       Target = stream;
+       if (ptr == NULL)
+               return 0;
+
+       StrBufAppendBufPlain(Target, ptr, size * nmemb, 0);
+       return size * nmemb;
+}
+
 
 /** 
  * \brief Escape a string for feeding out as a URL while appending it to a Buffer
@@ -746,7 +763,7 @@ long StrEscAppend(StrBuf *Target, const StrBuf *Source, const char *PlainIn, int
  * \param Source       source buffer; set to NULL if you just have a C-String
  * \param PlainIn       Plain-C string to append; set to NULL if unused
  */
-void StrMsgEscAppend(StrBuf *Target, StrBuf *Source, const char *PlainIn)
+void StrMsgEscAppend(StrBuf *Target, const StrBuf *Source, const char *PlainIn)
 {
        const char *aptr, *eiptr;
        char *tptr, *eptr;
@@ -803,6 +820,77 @@ void StrMsgEscAppend(StrBuf *Target, StrBuf *Source, const char *PlainIn)
        *tptr = '\0';
 }
 
+
+
+/*
+ * \brief Append a string, escaping characters which have meaning in ICAL.  
+ * [\n,] 
+ * \param Target       target buffer
+ * \param Source       source buffer; set to NULL if you just have a C-String
+ * \param PlainIn       Plain-C string to append; set to NULL if unused
+ */
+void StrIcalEscAppend(StrBuf *Target, const StrBuf *Source, const char *PlainIn)
+{
+       const char *aptr, *eiptr;
+       char *tptr, *eptr;
+       long len;
+
+       if (((Source == NULL) && (PlainIn == NULL)) || (Target == NULL) )
+               return ;
+
+       if (PlainIn != NULL) {
+               aptr = PlainIn;
+               len = strlen(PlainIn);
+               eiptr = aptr + len;
+       }
+       else {
+               aptr = Source->buf;
+               eiptr = aptr + Source->BufUsed;
+               len = Source->BufUsed;
+       }
+
+       if (len == 0) 
+               return;
+
+       eptr = Target->buf + Target->BufSize - 8; 
+       tptr = Target->buf + Target->BufUsed;
+       
+       while (aptr < eiptr){
+               if(tptr + 3 >= eptr) {
+                       IncreaseBuf(Target, 1, -1);
+                       eptr = Target->buf + Target->BufSize - 8; 
+                       tptr = Target->buf + Target->BufUsed;
+               }
+              
+               if (*aptr == '\n') {
+                       *tptr = '\\';
+                       Target->BufUsed++;
+                       tptr++;
+                       *tptr = 'n';
+                       Target->BufUsed++;
+               }
+               else if (*aptr == '\r') {
+                       *tptr = '\\';
+                       Target->BufUsed++;
+                       tptr++;
+                       *tptr = 'r';
+                       Target->BufUsed++;
+               }
+               else if (*aptr == ',') {
+                       *tptr = '\\';
+                       Target->BufUsed++;
+                       tptr++;
+                       *tptr = ',';
+                       Target->BufUsed++;
+               } else {
+                       *tptr = *aptr;
+                       Target->BufUsed++;
+               }
+               tptr++; aptr++;
+       }
+       *tptr = '\0';
+}
+
 /*
  * \brief Append a string, escaping characters which have meaning in JavaScript strings .  
  *