Skip to content

Commit 0323144

Browse files
authored
Merge pull request #138 from flrdv/master
Fixed bugs with no calling OnBind callback, infinite loop causing frozen connections in static files distribution
2 parents e20ef64 + 0de0999 commit 0323144

File tree

6 files changed

+24
-31
lines changed

6 files changed

+24
-31
lines changed

indi.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ func (a *App) OnStart(cb func()) *App {
4949

5050
// OnBind calls the passed callback for every address, that was bound without any errors
5151
func (a *App) OnBind(cb func(addr string)) *App {
52-
a.hooks.OnListenerStart = cb
52+
a.hooks.OnBind = cb
5353
return a
5454
}
5555

@@ -136,6 +136,10 @@ func (a *App) bind(r router.Router) ([]*tcp.Server, error) {
136136
src.Handler = a.newHTTPHandler(encryption.Plain)
137137
}
138138

139+
if a.hooks.OnBind != nil {
140+
a.hooks.OnBind(src.Addr)
141+
}
142+
139143
servers = append(servers, tcp.NewServer(listener, func(conn net.Conn) {
140144
src.Handler(conn, r)
141145
}))
@@ -195,9 +199,9 @@ func (a *App) newHTTPHandler(enc encryption.Token) listenerHandler {
195199
}
196200

197201
type hooks struct {
198-
OnStart func()
199-
OnListenerStart func(addr string)
200-
OnStop func()
202+
OnStart func()
203+
OnBind func(addr string)
204+
OnStop func()
201205
}
202206

203207
func callIfNotNil(f func()) {

internal/httpchars/chars.go

Lines changed: 0 additions & 13 deletions
This file was deleted.

internal/protocol/http1/serializer.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
package http1
22

33
import (
4+
"fmt"
45
"github.com/indigo-web/indigo/http"
56
"github.com/indigo-web/indigo/http/cookie"
67
"github.com/indigo-web/indigo/http/headers"
78
"github.com/indigo-web/indigo/http/method"
89
"github.com/indigo-web/indigo/http/proto"
910
"github.com/indigo-web/indigo/http/status"
10-
"github.com/indigo-web/indigo/internal/httpchars"
1111
"github.com/indigo-web/indigo/internal/response"
1212
"github.com/indigo-web/utils/strcomp"
13-
"github.com/indigo-web/utils/uf"
1413
"io"
1514
"log"
1615
"strconv"
@@ -22,6 +21,7 @@ const (
2221
transferEncoding = "Transfer-Encoding: "
2322
contentLength = "Content-Length: "
2423
setCookie = "Set-Cookie: "
24+
crlf = "\r\n"
2525
)
2626

2727
// minimalFileBuffSize defines the minimal size of the file buffer. In case it's less
@@ -243,8 +243,8 @@ func (d *Serializer) writeChunkedBody(r io.Reader, writer Writer) error {
243243
// offset for it
244244
blankSpace := hexValueOffset - len(buff)
245245
copy(d.fileBuff[blankSpace:], buff)
246-
copy(d.fileBuff[hexValueOffset:], httpchars.CRLF)
247-
copy(d.fileBuff[buffOffset+n:], httpchars.CRLF)
246+
copy(d.fileBuff[hexValueOffset:], crlf)
247+
copy(d.fileBuff[buffOffset+n:], crlf)
248248

249249
if err := writer.Write(d.fileBuff[blankSpace : buffOffset+n+crlfSize]); err != nil {
250250
return status.ErrCloseConnection
@@ -346,11 +346,11 @@ func (d *Serializer) sp() {
346346
}
347347

348348
func (d *Serializer) colonsp() {
349-
d.buff = append(d.buff, httpchars.COLONSP...)
349+
d.buff = append(d.buff, ':', ' ')
350350
}
351351

352352
func (d *Serializer) crlf() {
353-
d.buff = append(d.buff, httpchars.CRLF...)
353+
d.buff = append(d.buff, crlf...)
354354
}
355355

356356
func (d *Serializer) clear() {
@@ -389,7 +389,8 @@ func processDefaultHeaders(hdrs map[string]string) defaultHeaders {
389389
}
390390

391391
func renderHeader(key, value string) string {
392-
return key + httpchars.COLONSP + value + uf.B2S(httpchars.CRLF)
392+
// this is done once at serializer construction, so pretty much acceptable
393+
return fmt.Sprintf("%s: %s\r\n", key, value)
393394
}
394395

395396
type defaultHeader struct {

router/inbuilt/static.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ func isSafe(path string) bool {
3434
if dot < len(path)-1 && path[dot+1] == '.' {
3535
return false
3636
}
37+
38+
path = path[dot+1:]
3739
}
3840

3941
return true

router/inbuilt/trace.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package inbuilt
33
import (
44
"github.com/indigo-web/indigo/http"
55
"github.com/indigo-web/indigo/http/headers"
6-
"github.com/indigo-web/indigo/internal/httpchars"
76
"strings"
87
)
98

@@ -20,11 +19,11 @@ func traceResponse(respond *http.Response, messageBody []byte) *http.Response {
2019

2120
func renderHTTPRequest(request *http.Request, buff []byte) []byte {
2221
buff = append(buff, request.Method.String()...)
23-
buff = append(buff, httpchars.SP...)
22+
buff = append(buff, ' ')
2423
buff = requestURI(request, buff)
25-
buff = append(buff, httpchars.SP...)
24+
buff = append(buff, ' ')
2625
buff = append(buff, strings.TrimSpace(request.Proto.String())...)
27-
buff = append(buff, httpchars.CRLF...)
26+
buff = append(buff, "\r\n"...)
2827
buff = requestHeaders(request.Headers, buff)
2928
buff = append(buff, "Content-Length: 0\r\n\r\n"...)
3029

@@ -44,8 +43,8 @@ func requestURI(request *http.Request, buff []byte) []byte {
4443

4544
func requestHeaders(hdrs headers.Headers, buff []byte) []byte {
4645
for _, pair := range hdrs.Unwrap() {
47-
buff = append(append(buff, pair.Key...), httpchars.COLONSP...)
48-
buff = append(append(buff, pair.Value...), httpchars.CRLF...)
46+
buff = append(append(buff, pair.Key...), ": "...)
47+
buff = append(append(buff, pair.Value...), "\r\n"...)
4948
}
5049

5150
return buff

version.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
package indigo
22

3-
const Version = "v0.16.2"
3+
const Version = "v0.16.3"

0 commit comments

Comments
 (0)