Skip to content

Commit 12a91a1

Browse files
committed
Add httpGetCookieValue API.
1 parent c4b4386 commit 12a91a1

6 files changed

Lines changed: 162 additions & 28 deletions

File tree

CHANGES.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ libcups v3.0.0 (YYYY-MM-DD)
55
---------------------------
66

77
- Added `cupsOAuthGetJWKS` API.
8-
- Added `httpGetSecurity` API.
8+
- Added `httpGetCookieValue` and `httpGetSecurity` APIs.
99
- Updated `ippfind` to use `cupsGetClock` API.
1010
- Fixed return values of `ippDateToTime` when the timezone isn't GMT.
1111
- Fixed a potential timing issue with `cupsEnumDests`.

cups/http.c

Lines changed: 104 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -840,7 +840,11 @@ httpGetContentEncoding(http_t *http) // I - HTTP connection
840840

841841

842842
//
843-
// 'httpGetCookie()' - Get any cookie data from the response.
843+
// 'httpGetCookie()' - Get "Cookie:" data from the HTTP connection.
844+
//
845+
// This function returns any HTTP "Cookie:" header data for the given HTTP
846+
// connection as described in RFC 6265. Use the @link httpGetCookieValue@ to
847+
// get the value of a named cookie.
844848
//
845849

846850
const char * // O - Cookie data or `NULL`
@@ -850,6 +854,105 @@ httpGetCookie(http_t *http) // I - HTTP connection
850854
}
851855

852856

857+
//
858+
// 'httpGetCookieValue()' - Get the value of a named cookie from the HTTP connection.
859+
//
860+
// This function copies the value of a named cookie in the HTTP "Cookie:" header
861+
// for the given HTTP connection as described in RFC 6265. Use the
862+
// @link httpGetCookie@ function to get the original "Cookie:" string.
863+
//
864+
865+
char * // O - Cookie value or `NULL` if not present
866+
httpGetCookieValue(http_t *http, // I - HTTP connection
867+
const char *name, // I - Cookie name
868+
char *buffer, // I - Value buffer
869+
size_t bufsize) // I - Size of value buffer
870+
{
871+
const char *cookie; // Cookie: header value
872+
char current[128], // Current name string
873+
*ptr, // Pointer into name/buffer
874+
*end; // End of name/buffer
875+
bool match; // Does the current name match?
876+
877+
878+
// Range check input...
879+
if (buffer)
880+
*buffer = '\0';
881+
882+
if (!http || !http->cookie || !name || !buffer || bufsize < 2)
883+
return (NULL);
884+
885+
// Loop through the cookie string...
886+
for (cookie = http->cookie; *cookie;)
887+
{
888+
// Skip leading whitespace...
889+
while (isspace(*cookie & 255))
890+
cookie ++;
891+
if (!*cookie)
892+
break;
893+
894+
// Copy the name...
895+
for (ptr = current, end = current + sizeof(current) - 1; *cookie && *cookie != '=';)
896+
{
897+
if (ptr < end)
898+
*ptr++ = *cookie++;
899+
else
900+
cookie ++;
901+
}
902+
903+
if (*cookie != '=')
904+
break;
905+
906+
*ptr = '\0';
907+
match = !strcmp(current, name);
908+
cookie ++;
909+
910+
// Then the value...
911+
if (*cookie == '\"')
912+
{
913+
// Copy quoted value...
914+
for (cookie ++, ptr = buffer, end = buffer + bufsize - 1; *cookie && *cookie != '\"';)
915+
{
916+
if (match && ptr < end)
917+
*ptr++ = *cookie++;
918+
else
919+
cookie ++;
920+
}
921+
922+
if (*cookie == '\"')
923+
cookie ++;
924+
else
925+
match = false;
926+
}
927+
else
928+
{
929+
// Copy unquoted value...
930+
for (ptr = buffer, end = buffer + bufsize - 1; *cookie && *cookie != ';';)
931+
{
932+
if (match && ptr < end)
933+
*ptr++ = *cookie++;
934+
else
935+
cookie ++;
936+
}
937+
}
938+
939+
if (match)
940+
{
941+
// Got the value we were looking for, nul-terminate and return...
942+
*ptr = '\0';
943+
return (buffer);
944+
}
945+
946+
// Skip over separator...
947+
if (*cookie == ';')
948+
cookie ++;
949+
}
950+
951+
// If we get here then we never found the cookie...
952+
return (NULL);
953+
}
954+
955+
853956
//
854957
// 'httpGetEncryption()' - Get the current encryption mode of a connection.
855958
//

cups/http.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -456,6 +456,7 @@ extern const char *httpGetAuthString(http_t *http) _CUPS_PUBLIC;
456456
extern bool httpGetBlocking(http_t *http) _CUPS_PUBLIC;
457457
extern const char *httpGetContentEncoding(http_t *http) _CUPS_PUBLIC;
458458
extern const char *httpGetCookie(http_t *http) _CUPS_PUBLIC;
459+
extern char *httpGetCookieValue(http_t *http, const char *name, char *buffer, size_t bufsize) _CUPS_PUBLIC;
459460
extern const char *httpGetDateString(time_t t, char *s, size_t slen) _CUPS_PUBLIC;
460461
extern time_t httpGetDateTime(const char *s) _CUPS_PUBLIC;
461462
extern http_encryption_t httpGetEncryption(http_t *http) _CUPS_PUBLIC;

cups/libcups3.def

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
LIBRARY libcups3
22
VERSION 3.0
33
EXPORTS
4+
_cups_debug_set
5+
_cups_gettimeofday
6+
_cups_hstrerror
7+
_cups_strcasecmp
8+
_cups_strcpy
9+
_cups_strcpy
10+
_cups_strncasecmp
411
_cupsBufferGet
512
_cupsBufferRelease
613
_cupsConnect
@@ -13,8 +20,8 @@ _cupsGetDests
1320
_cupsGetPassword
1421
_cupsGetUserDefault
1522
_cupsGlobalLock
16-
_cupsGlobalUnlock
1723
_cupsGlobals
24+
_cupsGlobalUnlock
1825
_cupsJSONDelete
1926
_cupsMD5Append
2027
_cupsMD5Finish
@@ -33,13 +40,6 @@ _cupsStrFree
3340
_cupsStrRetain
3441
_cupsStrScand
3542
_cupsStrStatistics
36-
_cups_debug_set
37-
_cups_gettimeofday
38-
_cups_hstrerror
39-
_cups_strcasecmp
40-
_cups_strcpy
41-
_cups_strcpy
42-
_cups_strncasecmp
4343
_httpCreateCredentials
4444
_httpDecodeURI
4545
_httpDisconnect
@@ -99,6 +99,7 @@ cupsCondWait
9999
cupsConnectDest
100100
cupsCopyCredentials
101101
cupsCopyCredentialsKey
102+
cupsCopyCredentialsPublicKey
102103
cupsCopyCredentialsRequest
103104
cupsCopyDest
104105
cupsCopyDestConflicts
@@ -109,6 +110,10 @@ cupsCreateCredentialsRequest
109110
cupsCreateDestJob
110111
cupsCreateTempFd
111112
cupsCreateTempFile
113+
cupsDirClose
114+
cupsDirOpen
115+
cupsDirRead
116+
cupsDirRewind
112117
cupsDNSSDAssembleFullName
113118
cupsDNSSDBrowseDelete
114119
cupsDNSSDBrowseGetContext
@@ -133,10 +138,6 @@ cupsDNSSDServiceGetName
133138
cupsDNSSDServiceNew
134139
cupsDNSSDServicePublish
135140
cupsDNSSDServiceSetLocation
136-
cupsDirClose
137-
cupsDirOpen
138-
cupsDirRead
139-
cupsDirRewind
140141
cupsDoAuthentication
141142
cupsDoFileRequest
142143
cupsDoIORequest
@@ -177,10 +178,10 @@ cupsFindDestDefault
177178
cupsFindDestReady
178179
cupsFindDestSupported
179180
cupsFinishDestDocument
180-
cupsFormDecode
181-
cupsFormEncode
182181
cupsFormatString
183182
cupsFormatStringv
183+
cupsFormDecode
184+
cupsFormEncode
184185
cupsFreeDestInfo
185186
cupsFreeDests
186187
cupsFreeJobs
@@ -196,8 +197,8 @@ cupsGetDestMediaByName
196197
cupsGetDestMediaBySize
197198
cupsGetDestMediaCount
198199
cupsGetDestMediaDefault
199-
cupsGetDestWithURI
200200
cupsGetDests
201+
cupsGetDestWithURI
201202
cupsGetEncryption
202203
cupsGetError
203204
cupsGetErrorString
@@ -213,9 +214,9 @@ cupsGetResponse
213214
cupsGetServer
214215
cupsGetUser
215216
cupsGetUserAgent
216-
cupsHMACData
217217
cupsHashData
218218
cupsHashString
219+
cupsHMACData
219220
cupsJSONAdd
220221
cupsJSONDelete
221222
cupsJSONExportFile
@@ -240,17 +241,18 @@ cupsJWTDelete
240241
cupsJWTExportString
241242
cupsJWTGetAlgorithm
242243
cupsJWTGetClaimNumber
244+
cupsJWTGetClaims
243245
cupsJWTGetClaimString
244246
cupsJWTGetClaimType
245247
cupsJWTGetClaimValue
246-
cupsJWTGetClaims
247248
cupsJWTGetHeaderNumber
249+
cupsJWTGetHeaders
248250
cupsJWTGetHeaderString
249251
cupsJWTGetHeaderType
250252
cupsJWTGetHeaderValue
251-
cupsJWTGetHeaders
252253
cupsJWTHasValidSignature
253254
cupsJWTImportString
255+
cupsJWTLoadCredentials
254256
cupsJWTMakePrivateKey
255257
cupsJWTMakePublicKey
256258
cupsJWTNew
@@ -289,6 +291,7 @@ cupsOAuthCopyRefreshToken
289291
cupsOAuthCopyUserId
290292
cupsOAuthGetAuthorizationCode
291293
cupsOAuthGetClientId
294+
cupsOAuthGetJWKS
292295
cupsOAuthGetMetadata
293296
cupsOAuthGetTokens
294297
cupsOAuthMakeAuthorizationURL
@@ -298,11 +301,6 @@ cupsOAuthSaveTokens
298301
cupsParseOptions
299302
cupsPutFd
300303
cupsPutFile
301-
cupsRWDestroy
302-
cupsRWInit
303-
cupsRWLockRead
304-
cupsRWLockWrite
305-
cupsRWUnlock
306304
cupsRasterClose
307305
cupsRasterGetErrorString
308306
cupsRasterInitHeader
@@ -315,6 +313,11 @@ cupsRasterWritePixels
315313
cupsReadResponseData
316314
cupsRemoveDest
317315
cupsRemoveOption
316+
cupsRWDestroy
317+
cupsRWInit
318+
cupsRWLockRead
319+
cupsRWLockWrite
320+
cupsRWUnlock
318321
cupsSaveCredentials
319322
cupsSendRequest
320323
cupsSetClientCredentials
@@ -374,6 +377,7 @@ httpGetAuthString
374377
httpGetBlocking
375378
httpGetContentEncoding
376379
httpGetCookie
380+
httpGetCookieValue
377381
httpGetDateString
378382
httpGetDateTime
379383
httpGetEncryption
@@ -387,12 +391,12 @@ httpGetLength
387391
httpGetPending
388392
httpGetReady
389393
httpGetRemaining
394+
httpGets
390395
httpGetSecurity
391396
httpGetState
392397
httpGetStatus
393398
httpGetSubField
394399
httpGetVersion
395-
httpGets
396400
httpInitialize
397401
httpIsChunked
398402
httpIsEncrypted
@@ -416,8 +420,8 @@ httpSetTimeout
416420
httpShutdown
417421
httpStateString
418422
httpStatusString
419-
httpURIStatusString
420423
httpUpdate
424+
httpURIStatusString
421425
httpWait
422426
httpWrite
423427
httpWriteRequest

doc/cupspm.epub

156 Bytes
Binary file not shown.

doc/cupspm.html

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -607,6 +607,7 @@ <h2 class="title">Contents</h2>
607607
<li><a href="#httpGetBlocking">httpGetBlocking</a></li>
608608
<li><a href="#httpGetContentEncoding">httpGetContentEncoding</a></li>
609609
<li><a href="#httpGetCookie">httpGetCookie</a></li>
610+
<li><a href="#httpGetCookieValue">httpGetCookieValue</a></li>
610611
<li><a href="#httpGetDateString">httpGetDateString</a></li>
611612
<li><a href="#httpGetDateTime">httpGetDateTime</a></li>
612613
<li><a href="#httpGetEncryption">httpGetEncryption</a></li>
@@ -7781,7 +7782,7 @@ <h4 class="discussion">Discussion</h4>
77817782
client. The value returned can be use in subsequent requests (for clients)
77827783
or in the response (for servers) in order to compress the content stream.</p>
77837784
<h3 class="function"><a id="httpGetCookie">httpGetCookie</a></h3>
7784-
<p class="description">Get any cookie data from the response.</p>
7785+
<p class="description">Get &quot;Cookie:&quot; data from the HTTP connection.</p>
77857786
<p class="code">
77867787
<span class="reserved">const</span> <span class="reserved">char</span> *httpGetCookie(<a href="#http_t">http_t</a> *http);</p>
77877788
<h4 class="parameters">Parameters</h4>
@@ -7791,6 +7792,31 @@ <h4 class="parameters">Parameters</h4>
77917792
</tbody></table>
77927793
<h4 class="returnvalue">Return Value</h4>
77937794
<p class="description">Cookie data or <code>NULL</code></p>
7795+
<h4 class="discussion">Discussion</h4>
7796+
<p class="discussion">This function returns any HTTP &quot;Cookie:&quot; header data for the given HTTP
7797+
connection as described in RFC 6265. Use the <a href="#httpGetCookieValue"><code>httpGetCookieValue</code></a> to
7798+
get the value of a named cookie.</p>
7799+
<h3 class="function"><a id="httpGetCookieValue">httpGetCookieValue</a></h3>
7800+
<p class="description">Get the value of a named cookie from the HTTP connection.</p>
7801+
<p class="code">
7802+
<span class="reserved">char</span> *httpGetCookieValue(<a href="#http_t">http_t</a> *http, <span class="reserved">const</span> <span class="reserved">char</span> *name, <span class="reserved">char</span> *buffer, size_t bufsize);</p>
7803+
<h4 class="parameters">Parameters</h4>
7804+
<table class="list"><tbody>
7805+
<tr><th>http</th>
7806+
<td class="description">HTTP connection</td></tr>
7807+
<tr><th>name</th>
7808+
<td class="description">Cookie name</td></tr>
7809+
<tr><th>buffer</th>
7810+
<td class="description">Value buffer</td></tr>
7811+
<tr><th>bufsize</th>
7812+
<td class="description">Size of value buffer</td></tr>
7813+
</tbody></table>
7814+
<h4 class="returnvalue">Return Value</h4>
7815+
<p class="description">Cookie value or <code>NULL</code> if not present</p>
7816+
<h4 class="discussion">Discussion</h4>
7817+
<p class="discussion">This function copies the value of a named cookie in the HTTP &quot;Cookie:&quot; header
7818+
for the given HTTP connection as described in RFC 6265. Use the
7819+
<a href="#httpGetCookie"><code>httpGetCookie</code></a> function to get the original &quot;Cookie:&quot; string.</p>
77947820
<h3 class="function"><a id="httpGetDateString">httpGetDateString</a></h3>
77957821
<p class="description">Get a formatted date/time string from a time value.</p>
77967822
<p class="code">

0 commit comments

Comments
 (0)