websocket-server – Blame information for rev 1
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | office | 1 | using System; |
2 | using System.Collections.Generic; |
||
3 | using System.Linq; |
||
4 | using System.Text; |
||
5 | using System.IO; |
||
6 | using System.Net.Sockets; |
||
7 | using System.Text.RegularExpressions; |
||
8 | using System.Diagnostics; |
||
9 | using System.Threading; |
||
10 | using WebSockets.Exceptions; |
||
11 | |||
12 | namespace WebSockets.Server.Http |
||
13 | { |
||
14 | public class HttpHelper |
||
15 | { |
||
16 | public static string ReadHttpHeader(Stream stream) |
||
17 | { |
||
18 | int length = 1024*16; // 16KB buffer more than enough for http header |
||
19 | byte[] buffer = new byte[length]; |
||
20 | int offset = 0; |
||
21 | int bytesRead = 0; |
||
22 | do |
||
23 | { |
||
24 | if (offset >= length) |
||
25 | { |
||
26 | throw new EntityTooLargeException("Http header message too large to fit in buffer (16KB)"); |
||
27 | } |
||
28 | |||
29 | bytesRead = stream.Read(buffer, offset, length - offset); |
||
30 | offset += bytesRead; |
||
31 | string header = Encoding.UTF8.GetString(buffer, 0, offset); |
||
32 | |||
33 | // as per http specification, all headers should end this this |
||
34 | if (header.Contains("\r\n\r\n")) |
||
35 | { |
||
36 | return header; |
||
37 | } |
||
38 | |||
39 | } while (bytesRead > 0); |
||
40 | |||
41 | return string.Empty; |
||
42 | } |
||
43 | |||
44 | public static void WriteHttpHeader(string response, Stream stream) |
||
45 | { |
||
46 | response = response.Trim() + Environment.NewLine + Environment.NewLine; |
||
47 | Byte[] bytes = Encoding.UTF8.GetBytes(response); |
||
48 | stream.Write(bytes, 0, bytes.Length); |
||
49 | } |
||
50 | } |
||
51 | } |