Zig 0.17.0-dev (Split by item) This is an example of documentation generated by
ZigDoc , an alternative to Zig's built-in
Auto Doc feature. See also
examples in other modes/formats . The project being documented here (as the example) is the Zig standard library.
std › http › Server › Request › respondWebSocket Public Function respondWebSocket Doc Source 531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566 pub fn respondWebSocket (request : *Request , options : WebSocketOptions ) ExpectContinueError !WebSocket {
if (request .head .expect != null ) return error .HttpExpectationFailed ;
const out = request .server .out ;
const version : http .Version = .@"HTTP/1.1" ;
const status : http .Status = .switching_protocols ;
const phrase = options .reason orelse status .phrase () orelse "" ;
assert (request .head .version == version );
assert (request .head .method == .GET );
var sha1 = std .crypto .hash .Sha1 .init (.{});
sha1 .update (options .key );
sha1 .update ("258EAFA5-E914-47DA-95CA-C5AB0DC85B11" );
var digest : [std .crypto .hash .Sha1 .digest_length ]u8 = undefined ;
sha1 .final (&digest );
try out .print ("{s} {d} {s}\r\n" , .{ @tagName (version ), @backingInt (status ), phrase });
try out .writeAll ("connection: upgrade\r\nupgrade: websocket\r\nsec-websocket-accept: " );
const base64_digest = try out .writableArray (28 );
assert (std .base64 .standard .Encoder .encode (base64_digest , &digest ).len == base64_digest .len );
try out .writeAll ("\r\n" );
for (options .extra_headers ) |header | {
assert (header .name .len != 0 );
var bufs : [4 ][]const u8 = .{ header .name , ": " , header .value , "\r\n" };
try out .writeVecAll (&bufs );
}
try out .writeAll ("\r\n" );
return .{
.input = request .server .reader .in ,
.output = request .server .out ,
.key = options .key ,
};
}
The header is not guaranteed to be sent until
WebSocket.flushis called on the returned struct.