Artifactspublic-v0

curl

master
ply4 git calls27 ms289 KiB3 cachedprotocol v2
134 lines4.9 KiBRaw

WebSocket in curl

URL

WebSocket communication with libcurl is done by setting up a transfer to a URL using the ws:// or wss:// URL schemes. The latter one being the secure version done over HTTPS.

When using wss:// to do WebSocket over HTTPS, the standard TLS and HTTPS options are acknowledged for the CA, verification of server certificate etc.

WebSocket communication is done by upgrading a connection from either HTTP or HTTPS. When given a WebSocket URL to work with, libcurl considers it a transfer failure if the upgrade procedure fails. This means that a plain HTTP 200 response code is considered an error for this work.

API

The WebSocket API is described in the individual man pages for the new API.

WebSocket with libcurl can be done two ways.

  1. Get the WebSocket frames from the server sent to the write callback. You can then respond with curl_ws_send() from within the callback (or outside of it).

  2. Set CURLOPT_CONNECT_ONLY to 2L (new for WebSocket), which makes libcurl do an HTTP GET + Upgrade: request plus response in the curl_easy_perform() call before it returns and then you can use curl_ws_recv() and curl_ws_send() to receive and send WebSocket frames from and to the server.

The new options to curl_easy_setopt():

CURLOPT_WS_OPTIONS - to control specific behavior. CURLWS_RAW_MODE makes libcurl provide all WebSocket traffic raw in the callback. CURLWS_NOAUTOPONG disables automatic PONG replies.

The new function calls:

curl_ws_recv() - receive a WebSocket frame

curl_ws_send() - send a WebSocket frame

curl_ws_meta() - return WebSocket metadata within a write callback

Max frame size

The current implementation only supports frame sizes up to a max (64K right now). This is because the API delivers full frames and it then cannot manage the full 2^63 bytes size.

If we decide we need to support (much) larger frames than 64K, we need to adjust the API accordingly to be able to deliver partial frames in both directions.

Errors

If the given WebSocket URL (using ws:// or wss://) fails to get upgraded via a 101 response code and instead gets another response code back from the HTTP server - the transfer returns CURLE_HTTP_RETURNED_ERROR for that transfer. Note then that even 2xx response codes are then considered error since it failed to provide a WebSocket transfer.

Test suite

I looked for an existing small WebSocket server implementation with maximum flexibility to dissect and cram into the test suite but I ended up deciding that extending the existing test suite server sws to deal with WebSocket might be the better way.

  • This server is already integrated and working in the test suite

  • We want maximum control and ability to generate broken protocol and negative tests as well. A dumber and simpler TCP server could then be easier to massage into this than a "proper" WebSocket server.

Command line tool WebSocket

The plan is to make curl do WebSocket similar to telnet/nc. That part of the work has not been started.

Ideas:

  • Read stdin and send off as messages. Consider newline as end of fragment. (default to text? offer option to set binary)
  • Respond to PINGs automatically
  • Issue PINGs at some default interval (option to switch off/change interval?)
  • Allow -d to specify (initial) data to send (should the format allow for multiple separate frames?)
  • Exit after N messages received, where N can be zero.

Future work

  • Verify the Sec-WebSocket-Accept response. It requires a sha-1 function.
  • Verify Sec-WebSocket-Extensions and Sec-WebSocket-Protocol in the response
  • Consider a curl_ws_poll()
  • Make sure WebSocket code paths are fuzzed
  • Add client-side PING interval
  • Provide option to disable PING-PONG automation
  • Support compression (CURLWS_COMPRESS)

Why not libWebSocket

libWebSocket is said to be a solid, fast and efficient WebSocket library with a vast amount of users. My plan was originally to build upon it to skip having to implement the low level parts of WebSocket myself.

Here are the reasons why I have decided to move forward with WebSocket in curl without using libWebSocket:

  • doxygen generated docs only makes them hard to navigate. No tutorial, no clearly written explanatory pages for specific functions.

  • seems (too) tightly integrated with a specific TLS library, while we want to support WebSocket with whatever TLS library libcurl was already made to work with.

  • seems (too) tightly integrated with event libraries

  • the references to threads and thread-pools in code and APIs indicate too much logic for our purposes

  • "bloated" - it is a huge library that is actually more lines of code than libcurl itself

  • WebSocket is a fairly simple protocol on the network/framing layer so making a homegrown handling of it should be fine

1<!--
2Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
4SPDX-License-Identifier: curl
5-->
7# WebSocket in curl
9## URL
11WebSocket communication with libcurl is done by setting up a transfer to a URL
12using the `ws://` or `wss://` URL schemes. The latter one being the secure
13version done over HTTPS.
15When using `wss://` to do WebSocket over HTTPS, the standard TLS and HTTPS
16options are acknowledged for the CA, verification of server certificate etc.
18WebSocket communication is done by upgrading a connection from either HTTP or
19HTTPS. When given a WebSocket URL to work with, libcurl considers it a
20transfer failure if the upgrade procedure fails. This means that a plain HTTP
21200 response code is considered an error for this work.
23## API
25The WebSocket API is described in the individual man pages for the new API.
27WebSocket with libcurl can be done two ways.
291. Get the WebSocket frames from the server sent to the write callback. You
30 can then respond with `curl_ws_send()` from within the callback (or outside
31 of it).
332. Set `CURLOPT_CONNECT_ONLY` to 2L (new for WebSocket), which makes libcurl
34 do an HTTP GET + `Upgrade:` request plus response in the
35 `curl_easy_perform()` call before it returns and then you can use
36 `curl_ws_recv()` and `curl_ws_send()` to receive and send WebSocket frames
37 from and to the server.
39The new options to `curl_easy_setopt()`:
41`CURLOPT_WS_OPTIONS` - to control specific behavior. `CURLWS_RAW_MODE` makes
42libcurl provide all WebSocket traffic raw in the callback. `CURLWS_NOAUTOPONG`
43disables automatic `PONG` replies.
45The new function calls:
47`curl_ws_recv()` - receive a WebSocket frame
49`curl_ws_send()` - send a WebSocket frame
51`curl_ws_meta()` - return WebSocket metadata within a write callback
53## Max frame size
55The current implementation only supports frame sizes up to a max (64K right
56now). This is because the API delivers full frames and it then cannot manage
57the full 2^63 bytes size.
59If we decide we need to support (much) larger frames than 64K, we need to
60adjust the API accordingly to be able to deliver partial frames in both
61directions.
63## Errors
65If the given WebSocket URL (using `ws://` or `wss://`) fails to get upgraded
66via a 101 response code and instead gets another response code back from the
67HTTP server - the transfer returns `CURLE_HTTP_RETURNED_ERROR` for that
68transfer. Note then that even 2xx response codes are then considered error
69since it failed to provide a WebSocket transfer.
71## Test suite
73I looked for an existing small WebSocket server implementation with maximum
74flexibility to dissect and cram into the test suite but I ended up deciding
75that extending the existing test suite server sws to deal with WebSocket
76might be the better way.
78- This server is already integrated and working in the test suite
80- We want maximum control and ability to generate broken protocol and negative
81 tests as well. A dumber and simpler TCP server could then be easier to
82 massage into this than a "proper" WebSocket server.
84## Command line tool WebSocket
86The plan is to make curl do WebSocket similar to telnet/nc. That part of the
87work has not been started.
89Ideas:
91- Read stdin and send off as messages. Consider newline as end of fragment.
92 (default to text? offer option to set binary)
93- Respond to PINGs automatically
94- Issue PINGs at some default interval (option to switch off/change interval?)
95- Allow `-d` to specify (initial) data to send (should the format allow for
96 multiple separate frames?)
97- Exit after N messages received, where N can be zero.
99## Future work
101- Verify the Sec-WebSocket-Accept response. It requires a sha-1 function.
102- Verify Sec-WebSocket-Extensions and Sec-WebSocket-Protocol in the response
103- Consider a `curl_ws_poll()`
104- Make sure WebSocket code paths are fuzzed
105- Add client-side PING interval
106- Provide option to disable PING-PONG automation
107- Support compression (`CURLWS_COMPRESS`)
109## Why not libWebSocket
111libWebSocket is said to be a solid, fast and efficient WebSocket library with
112a vast amount of users. My plan was originally to build upon it to skip having
113to implement the low level parts of WebSocket myself.
115Here are the reasons why I have decided to move forward with WebSocket in
116curl **without using libWebSocket**:
118- doxygen generated docs only makes them hard to navigate. No tutorial, no
119 clearly written explanatory pages for specific functions.
121- seems (too) tightly integrated with a specific TLS library, while we want to
122 support WebSocket with whatever TLS library libcurl was already made to
123 work with.
125- seems (too) tightly integrated with event libraries
127- the references to threads and thread-pools in code and APIs indicate too
128 much logic for our purposes
130- "bloated" - it is a *huge* library that is actually more lines of code than
131 libcurl itself
133- WebSocket is a fairly simple protocol on the network/framing layer so
134 making a homegrown handling of it should be fine
Diagnostics4 git calls · 27 ms
CallStatusSizeTime027 ms
discover200152 B3 ms
git.upload-pack discover
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=ls-refs
git.upstream.dur_ms=3
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 · hit3 ms
git.upload-pack ls-refs
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=ls-refs
git.upstream.dur_ms=3
git.response.bytes=149
git.response.cache=HIT
git.response.duration_ms=3
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 · hit3 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=3
git.response.bytes=293470
git.response.cache=HIT
git.response.duration_ms=3
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
fetch2002.4 KiB · hit21 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=21
git.response.bytes=2450
git.response.cache=HIT
git.response.duration_ms=21
git.fetch.wants=b389475815914bc0e8607b7bbe287a71240e59e5
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=2450
git.pkt.payload.max=2429
git.sideband.band1.bytes=2428
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
a5e316d7eebecf5b
Request
fba0986b…
Protocol
v2 · sha1
Agent
gitty/1.0
Filter
advertised
Fetch
shallow filter sideband-all
Capabilities
agent ls-refs fetch object-format