From fbf54a663307b404bcbcde4dd455475b17020815 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Wilfried=20G=C3=B6esgens?= Date: Sun, 5 Jul 2009 20:25:36 +0000 Subject: [PATCH] * add cURL read-callbackhook, so we can read HTTP answers into StrBufs --- libcitadel/lib/libcitadel.h | 4 +- libcitadel/lib/stringbuf.c | 90 ++++++++++++++++++++++++++++++++++++- 2 files changed, 92 insertions(+), 2 deletions(-) diff --git a/libcitadel/lib/libcitadel.h b/libcitadel/lib/libcitadel.h index bae0441c3..a2337e8a8 100644 --- a/libcitadel/lib/libcitadel.h +++ b/libcitadel/lib/libcitadel.h @@ -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); diff --git a/libcitadel/lib/stringbuf.c b/libcitadel/lib/stringbuf.c index 8d665866c..599b482c0 100644 --- a/libcitadel/lib/stringbuf.c +++ b/libcitadel/lib/stringbuf.c @@ -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 . * -- 2.39.2