]> git.ipfire.org Git - thirdparty/curl.git/commitdiff
curl_ws_meta: initial implementation
authorDaniel Stenberg <daniel@haxx.se>
Fri, 9 Sep 2022 13:11:14 +0000 (15:11 +0200)
committerDaniel Stenberg <daniel@haxx.se>
Fri, 9 Sep 2022 13:11:14 +0000 (15:11 +0200)
docs/libcurl/curl_ws_meta.3
include/curl/websockets.h
lib/http.h
lib/ws.c

index 111de986e0a7b3f0e0ce3cf41e33c1d91979ecb8..4f3bc9cb77c823d01c6efda80968fee9aaa21f8a 100644 (file)
@@ -50,9 +50,17 @@ This function requires an easy handle as input argument for libcurl to know
 what transfer the question is about, but as there is no such pointer provided
 to the callback by libcurl itself, applications that want to use
 \fIcurl_ws_meta(3)\fP need to pass it on to the callback on its own.
+
+.SH "struct fields"
+.IP age
+This field specify the age of this struct. It is always zero for now.
+.IP recvflags
+This is a bitmask with the exact same meaning as the \fBrecvflags\fP
+documented for \fIcurl_ws_recv(3)\fP.
 .SH EXAMPLE
 .nf
 
+/* we pass a pointer to this struct to the callback */
 struct customdata {
   CURL *easy;
   void *ptr;
@@ -70,6 +78,7 @@ static size_t writecb(unsigned char *buffer,
 {
   struct customdata custom;
   custom.easy = easy;
+  custom.ptr = NULL;
   curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writecb);
   curl_easy_setopt(curl, CURLOPT_WRITEDATA, &custom);
 }
index dde0987e4dccc531cb3c199c528aab9174b6901a..c86e7b88fc0018b4b62f370e48c5962fc98cb2ba 100644 (file)
@@ -70,6 +70,6 @@ struct curl_ws_metadata {
   int recvflags; /* See the CURLWS_* defines */
 };
 
-struct curl_ws_metadata *curl_ws_meta(CURL *curl);
+CURL_EXTERN struct curl_ws_metadata *curl_ws_meta(CURL *curl);
 
 #endif /* CURLINC_WEBSOCKETS_H */
index a335eee23dcf12f0aeff01a79d896397e30666d1..0ab5924367e52875f42872b9bfa738bf7d352566 100644 (file)
@@ -209,6 +209,8 @@ struct websockets {
   struct dynbuf buf;
   size_t usedbuf; /* number of leading bytes in 'buf' the most recent complete
                      websocket frame uses */
+  struct curl_ws_metadata handout; /* the struct storage used for
+                                      curl_ws_meta() returns */
 };
 
 /****************************************************************************
index c8884126ca31ef3f716ae6a243c63d3da6b0b267..2ba0121138119756622b0a92b526bc73d021b9c7 100644 (file)
--- a/lib/ws.c
+++ b/lib/ws.c
@@ -335,8 +335,10 @@ size_t Curl_ws_writecb(char *buffer, size_t size /* 1 */,
         return result;
     }
     else {
-      /* TODO: store details about the frame in a struct to be reachable with
-         curl_ws_meta() from within the write callback */
+      /* Store details about the frame to be reachable with curl_ws_meta()
+         from within the write callback */
+      ws->ws.handout.age = 0;
+      ws->ws.handout.recvflags = recvflags;
 
       /* deliver the decoded frame to the user callback */
       if(data->set.fwrite_func((char *)wsp, 1, wslen, writebody_ptr) != wslen)
@@ -354,8 +356,9 @@ size_t Curl_ws_writecb(char *buffer, size_t size /* 1 */,
 }
 
 
-CURLcode curl_ws_recv(struct Curl_easy *data, void *buffer, size_t buflen,
-                      size_t *nread, unsigned int *recvflags)
+CURL_EXTERN CURLcode curl_ws_recv(struct Curl_easy *data, void *buffer,
+                                  size_t buflen,
+                                  size_t *nread, unsigned int *recvflags)
 {
   size_t bytes;
   CURLcode result;
@@ -523,9 +526,9 @@ static size_t ws_packet(struct Curl_easy *data,
   return outi;
 }
 
-CURLcode curl_ws_send(struct Curl_easy *data, const void *buffer,
-                      size_t buflen, size_t *sent,
-                      unsigned int sendflags)
+CURL_EXTERN CURLcode curl_ws_send(struct Curl_easy *data, const void *buffer,
+                                  size_t buflen, size_t *sent,
+                                  unsigned int sendflags)
 {
   size_t bytes;
   CURLcode result;
@@ -582,6 +585,16 @@ void Curl_ws_done(struct Curl_easy *data)
   Curl_dyn_free(&wsp->buf);
 }
 
+CURL_EXTERN struct curl_ws_metadata *curl_ws_meta(struct Curl_easy *data)
+{
+  /* we only return something for websockets, called from within the callback
+     when not using raw mode */
+  if(GOOD_EASY_HANDLE(data) && Curl_is_in_callback(data) && data->req.p.http &&
+     !data->set.ws_raw_mode)
+    return &data->req.p.http->ws.handout;
+  return NULL;
+}
+
 #else
 
 CURL_EXTERN CURLcode curl_ws_recv(CURL *curl, void *buffer, size_t buflen,
@@ -607,4 +620,9 @@ CURL_EXTERN CURLcode curl_ws_send(CURL *curl, const void *buffer,
   return CURLE_OK;
 }
 
+CURL_EXTERN struct curl_ws_metadata *curl_ws_meta(struct Curl_easy *data)
+{
+  (void)data;
+  return NULL;
+}
 #endif /* USE_WEBSOCKETS */