Skip to content

Commit

Permalink
Merge pull request indigo-web#138 from flrdv/master
Browse files Browse the repository at this point in the history
Fixed bugs with no calling OnBind callback, infinite loop causing frozen connections in static files distribution
  • Loading branch information
flrdv authored Apr 26, 2024
2 parents e20ef64 + 0de0999 commit 0323144
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 31 deletions.
12 changes: 8 additions & 4 deletions indi.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func (a *App) OnStart(cb func()) *App {

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

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

if a.hooks.OnBind != nil {
a.hooks.OnBind(src.Addr)
}

servers = append(servers, tcp.NewServer(listener, func(conn net.Conn) {
src.Handler(conn, r)
}))
Expand Down Expand Up @@ -195,9 +199,9 @@ func (a *App) newHTTPHandler(enc encryption.Token) listenerHandler {
}

type hooks struct {
OnStart func()
OnListenerStart func(addr string)
OnStop func()
OnStart func()
OnBind func(addr string)
OnStop func()
}

func callIfNotNil(f func()) {
Expand Down
13 changes: 0 additions & 13 deletions internal/httpchars/chars.go

This file was deleted.

15 changes: 8 additions & 7 deletions internal/protocol/http1/serializer.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
package http1

import (
"fmt"
"github.com/indigo-web/indigo/http"
"github.com/indigo-web/indigo/http/cookie"
"github.com/indigo-web/indigo/http/headers"
"github.com/indigo-web/indigo/http/method"
"github.com/indigo-web/indigo/http/proto"
"github.com/indigo-web/indigo/http/status"
"github.com/indigo-web/indigo/internal/httpchars"
"github.com/indigo-web/indigo/internal/response"
"github.com/indigo-web/utils/strcomp"
"github.com/indigo-web/utils/uf"
"io"
"log"
"strconv"
Expand All @@ -22,6 +21,7 @@ const (
transferEncoding = "Transfer-Encoding: "
contentLength = "Content-Length: "
setCookie = "Set-Cookie: "
crlf = "\r\n"
)

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

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

func (d *Serializer) colonsp() {
d.buff = append(d.buff, httpchars.COLONSP...)
d.buff = append(d.buff, ':', ' ')
}

func (d *Serializer) crlf() {
d.buff = append(d.buff, httpchars.CRLF...)
d.buff = append(d.buff, crlf...)
}

func (d *Serializer) clear() {
Expand Down Expand Up @@ -389,7 +389,8 @@ func processDefaultHeaders(hdrs map[string]string) defaultHeaders {
}

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

type defaultHeader struct {
Expand Down
2 changes: 2 additions & 0 deletions router/inbuilt/static.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ func isSafe(path string) bool {
if dot < len(path)-1 && path[dot+1] == '.' {
return false
}

path = path[dot+1:]
}

return true
Expand Down
11 changes: 5 additions & 6 deletions router/inbuilt/trace.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package inbuilt
import (
"github.com/indigo-web/indigo/http"
"github.com/indigo-web/indigo/http/headers"
"github.com/indigo-web/indigo/internal/httpchars"
"strings"
)

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

func renderHTTPRequest(request *http.Request, buff []byte) []byte {
buff = append(buff, request.Method.String()...)
buff = append(buff, httpchars.SP...)
buff = append(buff, ' ')
buff = requestURI(request, buff)
buff = append(buff, httpchars.SP...)
buff = append(buff, ' ')
buff = append(buff, strings.TrimSpace(request.Proto.String())...)
buff = append(buff, httpchars.CRLF...)
buff = append(buff, "\r\n"...)
buff = requestHeaders(request.Headers, buff)
buff = append(buff, "Content-Length: 0\r\n\r\n"...)

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

func requestHeaders(hdrs headers.Headers, buff []byte) []byte {
for _, pair := range hdrs.Unwrap() {
buff = append(append(buff, pair.Key...), httpchars.COLONSP...)
buff = append(append(buff, pair.Value...), httpchars.CRLF...)
buff = append(append(buff, pair.Key...), ": "...)
buff = append(append(buff, pair.Value...), "\r\n"...)
}

return buff
Expand Down
2 changes: 1 addition & 1 deletion version.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
package indigo

const Version = "v0.16.2"
const Version = "v0.16.3"

0 comments on commit 0323144

Please sign in to comment.