@marcel91200 / caddy Mirror · read-only
Fork 0

Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS

Mirrored from https://github.com/Slazee/caddy.git

Synced Nov 11, 02:15 UTC

master
go 145 lines 3.55 KB
Raw
1 // Copyright 2015 Matthew Holt and The Caddy Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 package fastcgi
16
17 import (
18 "bytes"
19 "encoding/binary"
20 )
21
22 // streamWriter abstracts out the separation of a stream into discrete records.
23 // It only writes maxWrite bytes at a time.
24 type streamWriter struct {
25 c *client
26 h header
27 buf *bytes.Buffer
28 recType uint8
29 }
30
31 func (w *streamWriter) writeRecord(recType uint8, content []byte) (err error) {
32 w.h.init(recType, w.c.reqID, len(content))
33 w.buf.Write(pad[:8])
34 w.writeHeader()
35 w.buf.Write(content)
36 w.buf.Write(pad[:w.h.PaddingLength])
37 _, err = w.buf.WriteTo(w.c.rwc)
38 return err
39 }
40
41 func (w *streamWriter) writeBeginRequest(role uint16, flags uint8) error {
42 b := [8]byte{byte(role >> 8), byte(role), flags}
43 return w.writeRecord(BeginRequest, b[:])
44 }
45
46 func (w *streamWriter) Write(p []byte) (int, error) {
47 // init header
48 if w.buf.Len() < 8 {
49 w.buf.Write(pad[:8])
50 }
51
52 nn := 0
53 for len(p) > 0 {
54 n := len(p)
55 nl := maxWrite + 8 - w.buf.Len()
56 if n > nl {
57 n = nl
58 w.buf.Write(p[:n])
59 if err := w.Flush(); err != nil {
60 return nn, err
61 }
62 // reset headers
63 w.buf.Write(pad[:8])
64 } else {
65 w.buf.Write(p[:n])
66 }
67 nn += n
68 p = p[n:]
69 }
70 return nn, nil
71 }
72
73 func (w *streamWriter) endStream() error {
74 // send empty record to close the stream
75 return w.writeRecord(w.recType, nil)
76 }
77
78 func (w *streamWriter) writePairs(pairs map[string]string) error {
79 b := make([]byte, 8)
80 nn := 0
81 // init headers
82 w.buf.Write(b)
83 for k, v := range pairs {
84 m := 8 + len(k) + len(v)
85 if m > maxWrite {
86 // param data size exceed 65535 bytes"
87 vl := maxWrite - 8 - len(k)
88 v = v[:vl]
89 }
90 n := encodeSize(b, uint32(len(k)))
91 n += encodeSize(b[n:], uint32(len(v)))
92 m = n + len(k) + len(v)
93 if (nn + m) > maxWrite {
94 if err := w.Flush(); err != nil {
95 return err
96 }
97 // reset headers
98 w.buf.Write(b)
99 nn = 0
100 }
101 nn += m
102 w.buf.Write(b[:n])
103 w.buf.WriteString(k)
104 w.buf.WriteString(v)
105 }
106 return w.FlushStream()
107 }
108
109 func encodeSize(b []byte, size uint32) int {
110 if size > 127 {
111 size |= 1 << 31
112 binary.BigEndian.PutUint32(b, size)
113 return 4
114 }
115 b[0] = byte(size) //nolint:gosec // false positive; b is made 8 bytes long, then this function is always called with b being at least 4 or 1 byte long
116 return 1
117 }
118
119 // writeHeader populate header wire data in buf, it abuses buffer.Bytes() modification
120 func (w *streamWriter) writeHeader() {
121 h := w.buf.Bytes()[:8]
122 h[0] = w.h.Version
123 h[1] = w.h.Type
124 binary.BigEndian.PutUint16(h[2:4], w.h.ID)
125 binary.BigEndian.PutUint16(h[4:6], w.h.ContentLength)
126 h[6] = w.h.PaddingLength
127 h[7] = w.h.Reserved
128 }
129
130 // Flush write buffer data to the underlying connection, it assumes header data is the first 8 bytes of buf
131 func (w *streamWriter) Flush() error {
132 w.h.init(w.recType, w.c.reqID, w.buf.Len()-8)
133 w.writeHeader()
134 w.buf.Write(pad[:w.h.PaddingLength])
135 _, err := w.buf.WriteTo(w.c.rwc)
136 return err
137 }
138
139 // FlushStream flush data then end current stream
140 func (w *streamWriter) FlushStream() error {
141 if err := w.Flush(); err != nil {
142 return err
143 }
144 return w.endStream()
145 }