Artifactspublic-v0

curl

master
ply4 git calls193 ms289 KiB2 cachedprotocol v2
150 lines3.8 KiBRaw

NAME

curl_ws_start_frame - start a new WebSocket frame

SYNOPSIS

c
#include <curl/curl.h>

CURLcode curl_ws_start_frame(CURL *curl,
                             unsigned int flags,
                             curl_off_t frame_len);

DESCRIPTION

Add the WebSocket frame header for the given flags and length to the transfers send buffer for WebSocket encoded data. Intended for use in a CURLOPT_READFUNCTION(3) callback.

When using a CURLOPT_READFUNCTION(3) in a WebSocket transfer, any data returned by that function is sent as a CURLWS_BINARY frame with the length being the amount of data read.

To send larger frames or frames of a different type, call curl_ws_start_frame() from within the read function and then return the data belonging to the frame.

The function fails, if a previous frame has not been completely read yet. Also it fails in CURLWS_RAW_MODE.

The read function in libcurl usually treats a return value of 0 as the end of file indication and stops any further reads. This would prevent sending WebSocket frames of length 0.

If the read function calls curl_ws_start_frame(), a return value of 0 is not treated as an end of file and libcurl calls the read function again.

FLAGS

Supports all flags documented in curl_ws_meta(3).

%PROTOCOLS%

EXAMPLE

c
#include <string.h> /* for strlen */

struct read_ctx {
  CURL *easy;
  const char *message;
  size_t msg_len;
  size_t nsent;
};

static size_t readcb(char *buf, size_t nitems, size_t buflen, void *p)
{
  struct read_ctx *ctx = p;
  size_t len = nitems * buflen;
  size_t left = ctx->msg_len - ctx->nsent;

  if(!ctx->nsent) {
    CURLcode result;
    /* Want to send TEXT frame. */
    result = curl_ws_start_frame(ctx->easy, CURLWS_TEXT,
                                 (curl_off_t)ctx->msg_len);
    if(result != CURLE_OK) {
      fprintf(stderr, "error starting frame: %d\n", (int)result);
      return CURL_READFUNC_ABORT;
    }
  }
  if(left) {
    if(left < len)
      len = left;
    memcpy(buf, ctx->message + ctx->nsent, len);
    ctx->nsent += len;
    return len;
  }
  return 0;
}

int main(void)
{
  CURL *easy;
  struct read_ctx rctx;
  CURLcode result;

  easy = curl_easy_init();
  if(!easy)
    return 1;

  curl_easy_setopt(easy, CURLOPT_URL, "wss://example.com");
  curl_easy_setopt(easy, CURLOPT_READFUNCTION, readcb);
  /* tell curl that we want to send the payload */
  memset(&rctx, 0, sizeof(rctx));
  rctx.easy = easy;
  rctx.message = "Hello, friend!";
  rctx.msg_len = strlen(rctx.message);
  curl_easy_setopt(easy, CURLOPT_READDATA, &rctx);
  curl_easy_setopt(easy, CURLOPT_UPLOAD, 1L);

  /* Perform the request, result gets the return code */
  result = curl_easy_perform(easy);
  /* Check for errors */
  if(result != CURLE_OK)
    fprintf(stderr, "curl_easy_perform() failed: %s\n",
            curl_easy_strerror(result));

  /* always cleanup */
  curl_easy_cleanup(easy);
  return 0;
}

%AVAILABILITY%

RETURN VALUE

This function returns a CURLcode indicating success or error.

CURLE_OK (0) means everything was OK, non-zero means an error occurred, see libcurl-errors(3). If CURLOPT_ERRORBUFFER(3) was set with curl_easy_setopt(3) there can be an error message stored in the error buffer when non-zero is returned.

Instead of blocking, the function returns CURLE_AGAIN. The correct behavior is then to wait for the socket to signal readability before calling this function again.

Any other non-zero return value indicates an error. See the libcurl-errors(3) man page for the full list with descriptions.

1---
2c: Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
3SPDX-License-Identifier: curl
4Title: curl_ws_start_frame
5Section: 3
6Source: libcurl
7See-also:
8 - curl_easy_getinfo (3)
9 - curl_easy_perform (3)
10 - curl_easy_setopt (3)
11 - curl_ws_recv (3)
12 - libcurl-ws (3)
13Protocol:
14 - WS
15Added-in: 8.16.0
16---
18# NAME
20curl_ws_start_frame - start a new WebSocket frame
22# SYNOPSIS
24~~~c
25#include <curl/curl.h>
27CURLcode curl_ws_start_frame(CURL *curl,
28 unsigned int flags,
29 curl_off_t frame_len);
30~~~
32# DESCRIPTION
34Add the WebSocket frame header for the given flags and length to
35the transfers send buffer for WebSocket encoded data. Intended for
36use in a CURLOPT_READFUNCTION(3) callback.
38When using a CURLOPT_READFUNCTION(3) in a WebSocket transfer, any
39data returned by that function is sent as a *CURLWS_BINARY* frame
40with the length being the amount of data read.
42To send larger frames or frames of a different type, call
43curl_ws_start_frame() from within the read function and then return
44the data belonging to the frame.
46The function fails, if a previous frame has not been completely read yet. Also
47it fails in *CURLWS_RAW_MODE*.
49The read function in libcurl usually treats a return value of 0 as the end of
50file indication and stops any further reads. This would prevent sending
51WebSocket frames of length 0.
53If the read function calls `curl_ws_start_frame()`, a return value of 0 is
54*not* treated as an end of file and libcurl calls the read function again.
56# FLAGS
58Supports all flags documented in curl_ws_meta(3).
60# %PROTOCOLS%
62# EXAMPLE
64~~~c
65#include <string.h> /* for strlen */
67struct read_ctx {
68 CURL *easy;
69 const char *message;
70 size_t msg_len;
71 size_t nsent;
72};
74static size_t readcb(char *buf, size_t nitems, size_t buflen, void *p)
76 struct read_ctx *ctx = p;
77 size_t len = nitems * buflen;
78 size_t left = ctx->msg_len - ctx->nsent;
80 if(!ctx->nsent) {
81 CURLcode result;
82 /* Want to send TEXT frame. */
83 result = curl_ws_start_frame(ctx->easy, CURLWS_TEXT,
84 (curl_off_t)ctx->msg_len);
85 if(result != CURLE_OK) {
86 fprintf(stderr, "error starting frame: %d\n", (int)result);
87 return CURL_READFUNC_ABORT;
88 }
89 }
90 if(left) {
91 if(left < len)
92 len = left;
93 memcpy(buf, ctx->message + ctx->nsent, len);
94 ctx->nsent += len;
95 return len;
96 }
97 return 0;
100int main(void)
102 CURL *easy;
103 struct read_ctx rctx;
104 CURLcode result;
106 easy = curl_easy_init();
107 if(!easy)
108 return 1;
110 curl_easy_setopt(easy, CURLOPT_URL, "wss://example.com");
111 curl_easy_setopt(easy, CURLOPT_READFUNCTION, readcb);
112 /* tell curl that we want to send the payload */
113 memset(&rctx, 0, sizeof(rctx));
114 rctx.easy = easy;
115 rctx.message = "Hello, friend!";
116 rctx.msg_len = strlen(rctx.message);
117 curl_easy_setopt(easy, CURLOPT_READDATA, &rctx);
118 curl_easy_setopt(easy, CURLOPT_UPLOAD, 1L);
120 /* Perform the request, result gets the return code */
121 result = curl_easy_perform(easy);
122 /* Check for errors */
123 if(result != CURLE_OK)
124 fprintf(stderr, "curl_easy_perform() failed: %s\n",
125 curl_easy_strerror(result));
127 /* always cleanup */
128 curl_easy_cleanup(easy);
129 return 0;
132~~~
134# %AVAILABILITY%
136# RETURN VALUE
138This function returns a CURLcode indicating success or error.
140CURLE_OK (0) means everything was OK, non-zero means an error occurred, see
141libcurl-errors(3). If CURLOPT_ERRORBUFFER(3) was set with curl_easy_setopt(3)
142there can be an error message stored in the error buffer when non-zero is
143returned.
145Instead of blocking, the function returns **CURLE_AGAIN**. The correct
146behavior is then to wait for the socket to signal readability before calling
147this function again.
149Any other non-zero return value indicates an error. See the libcurl-errors(3)
150man page for the full list with descriptions.
Diagnostics4 git calls · 193 ms
CallStatusSizeTime0193 ms
discover200152 B2 ms
git.upload-pack discover
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=2
git.response.bytes=152
git.server.agent=gitty/1.0
git.server.object_format=sha1
git.server.caps=agent ls-refs fetch object-format
git.server.fetch=shallow filter sideband-all
git.protocol.version=2
git.pkt.frames=8
git.pkt.data=6
git.pkt.flush=2
git.pkt.delim=0
git.pkt.bytes=152
git.pkt.payload.max=34
ls-refs200149 B · hit2 ms
git.upload-pack ls-refs
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=ls-refs
git.upstream.dur_ms=2
git.response.bytes=149
git.response.cache=HIT
git.response.duration_ms=2
git.protocol.version=2
git.pkt.frames=3
git.pkt.data=2
git.pkt.flush=1
git.pkt.delim=0
git.pkt.bytes=149
git.pkt.payload.max=78
git.ls_refs.count=2
git.ls_refs.head=refs/heads/master
fetchblob:none · deepen 10200287 KiB · hit2 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=189
git.response.bytes=293470
git.response.cache=HIT
git.response.duration_ms=2
git.fetch.filter=blob:none
git.fetch.filter.requested=tree:0
git.fetch.filter.fallback=blob:none
git.fetch.filter.learned=false
git.fetch.wants=31c911efe5652725e930f4d2e60c9060e5f6646d
git.fetch.haves.count=0
git.fetch.deepen=10
git.fetch.thin_pack=false
git.fetch.ofs_delta=true
git.protocol.version=2
git.pkt.frames=10
git.pkt.data=8
git.pkt.flush=1
git.pkt.delim=1
git.pkt.bytes=293470
git.pkt.payload.max=65516
git.sideband.band1.bytes=293354
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=87
git.pack.bytes=293354
git.pack.ofs_delta=0
git.pack.ref_delta=1
git.pack.chain.max=1
git.pack.trailer=6a646bcda6740bab42e321b51998b52ea15146c0
git.pack.full=86
Open pack map
fetch2001.8 KiB189 ms
git.upload-pack fetch
http.response.status_code=200
git.response.bytes=1850
git.response.cache=MISS
git.response.duration_ms=189
git.fetch.wants=9fcbae0274544fa00232d6cfbf62c80e4dccd0c6
git.fetch.haves.count=0
git.fetch.deepen=0
git.fetch.thin_pack=false
git.fetch.ofs_delta=true
git.protocol.version=2
git.pkt.frames=3
git.pkt.data=2
git.pkt.flush=1
git.pkt.delim=0
git.pkt.bytes=1850
git.pkt.payload.max=1829
git.sideband.band1.bytes=1828
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0

Each row is one HTTP round trip ply made to the git server for this page, on one timeline. discover asks what the server speaks, ls-refs lists branch tips, fetch brings objects by id. A hit was served by Workers Cache. Tap a row for its wire details.

Remote
https://5ad2236b39de4c8bd8f6270ea63f3ea9.artifacts.cloudflare.net/git/public-v0/curl.git
Ref
refs/heads/master
Commit
31c911efe5652725e930f4d2e60c9060e5f6646d
cf-ray
a5e33d699e27dafe
Request
ffbdb1b5…
Protocol
v2 · sha1
Agent
gitty/1.0
Filter
advertised
Fetch
shallow filter sideband-all
Capabilities
agent ls-refs fetch object-format