Skip to content

Commit 82ffdc4

Browse files
committed
go-aah/aah#157 code and flow optimization on http lib
1 parent 42580a0 commit 82ffdc4

8 files changed

+243
-189
lines changed

ahttp.go

+8
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,13 @@ const (
2424
MethodTrace = http.MethodTrace
2525
)
2626

27+
// URI Protocol scheme names
28+
const (
29+
SchemeHTTP = "http"
30+
SchemeHTTPS = "https"
31+
SchemeFTP = "ftp"
32+
)
33+
2734
// TimeFormat is the time format to use when generating times in HTTP
2835
// headers. It is like time.RFC1123 but hard-codes GMT as the time
2936
// zone. The time being formatted must be in UTC for Format to
@@ -53,6 +60,7 @@ func AcquireRequest(r *http.Request) *Request {
5360
// ReleaseRequest method resets the instance value and puts back to pool.
5461
func ReleaseRequest(r *Request) {
5562
if r != nil {
63+
r.cleanupMutlipart()
5664
r.Reset()
5765
requestPool.Put(r)
5866
}

content_type.go

+16-13
Original file line numberDiff line numberDiff line change
@@ -36,26 +36,29 @@ var (
3636

3737
// ContentTypeOctetStream content type for bytes.
3838
ContentTypeOctetStream = parseMediaType("application/octet-stream")
39-
)
4039

41-
type (
42-
// ContentType is represents request and response content type values
43-
ContentType struct {
44-
Mime string
45-
Exts []string
46-
Params map[string]string
47-
}
40+
// ContentTypeJavascript content type.
41+
ContentTypeJavascript = parseMediaType("application/javascript; charset=utf-8")
4842
)
4943

5044
//‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
51-
// Content-Type methods
45+
// Content-Type
5246
//___________________________________
5347

54-
// IsEqual method compares give Content-Type string with current instance.
48+
// ContentType is represents request and response content type values
49+
type ContentType struct {
50+
Mime string
51+
Exts []string
52+
Params map[string]string
53+
}
54+
55+
// IsEqual method returns true if its equals to current content-type instance
56+
// otherwise false.
5557
// E.g.:
5658
// contentType.IsEqual("application/json")
59+
// contentType.IsEqual("application/json; charset=utf-8")
5760
func (c *ContentType) IsEqual(contentType string) bool {
58-
return strings.HasPrefix(c.String(), strings.ToLower(contentType))
61+
return strings.HasPrefix(contentType, c.Mime)
5962
}
6063

6164
// Charset method returns charset of content-type
@@ -65,7 +68,7 @@ func (c *ContentType) IsEqual(contentType string) bool {
6568
//
6669
// Method returns `utf-8`
6770
func (c *ContentType) Charset(defaultCharset string) string {
68-
if v, ok := c.Params["charset"]; ok {
71+
if v, found := c.Params["charset"]; found {
6972
return v
7073
}
7174
return defaultCharset
@@ -117,6 +120,6 @@ func (c *ContentType) Raw() string {
117120
}
118121

119122
// String is stringer interface
120-
func (c *ContentType) String() string {
123+
func (c ContentType) String() string {
121124
return c.Raw()
122125
}

gzip_response.go

+8-10
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,6 @@ import (
1515
"aahframework.org/essentials.v0"
1616
)
1717

18-
// GzipResponse extends `ahttp.Response` and provides gzip for response
19-
// bytes before writing them to the underlying response.
20-
type GzipResponse struct {
21-
r *Response
22-
gw *gzip.Writer
23-
}
24-
2518
var (
2619
// GzipLevel holds value from app config.
2720
GzipLevel int
@@ -61,9 +54,16 @@ func PutGzipResponseWiriter(rw ResponseWriter) {
6154
}
6255

6356
//‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
64-
// Response methods
57+
// GzipResponse
6558
//___________________________________
6659

60+
// GzipResponse extends `ahttp.Response` to provides gzip compression for response
61+
// bytes to the underlying response.
62+
type GzipResponse struct {
63+
r *Response
64+
gw *gzip.Writer
65+
}
66+
6767
// Status method returns HTTP response status code. If status is not yet written
6868
// it reurns 0.
6969
func (g *GzipResponse) Status() int {
@@ -82,9 +82,7 @@ func (g *GzipResponse) Header() http.Header {
8282

8383
// Write method writes bytes into Response.
8484
func (g *GzipResponse) Write(b []byte) (int, error) {
85-
g.r.setContentTypeIfNotSet(b)
8685
g.r.WriteHeader(http.StatusOK)
87-
8886
size, err := g.gw.Write(b)
8987
g.r.bytesWritten += size
9088
return size, err

header.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -167,10 +167,9 @@ func NegotiateEncoding(req *http.Request) *AcceptSpec {
167167
// ParseContentType method parses the request header `Content-Type` as per RFC1521.
168168
func ParseContentType(req *http.Request) *ContentType {
169169
contentType := req.Header.Get(HeaderContentType)
170-
if ess.IsStrEmpty(contentType) {
170+
if contentType == "" {
171171
return ContentTypeHTML
172172
}
173-
174173
return parseMediaType(contentType)
175174
}
176175

@@ -281,7 +280,7 @@ func NewLocale(value string) *Locale {
281280
//___________________________________
282281

283282
// String is stringer interface.
284-
func (l *Locale) String() string {
283+
func (l Locale) String() string {
285284
return l.Raw
286285
}
287286

header_test.go

+9-6
Original file line numberDiff line numberDiff line change
@@ -94,26 +94,29 @@ func TestHTTPNegotiateLocale(t *testing.T) {
9494

9595
func TestHTTPNegotiateEncoding(t *testing.T) {
9696
req1 := createRawHTTPRequest(HeaderAcceptEncoding, "compress;q=0.5, gzip;q=1.0")
97-
encoding := NegotiateEncoding(req1)
97+
areq1 := AcquireRequest(req1)
98+
encoding := areq1.AcceptEncoding()
99+
assert.True(t, areq1.IsGzipAccepted)
98100
assert.Equal(t, "gzip", encoding.Value)
99101
assert.Equal(t, "gzip;q=1.0", encoding.Raw)
100-
assert.True(t, isGzipAccepted(&Request{}, req1))
101102

102103
req2 := createRawHTTPRequest(HeaderAcceptEncoding, "gzip;q=1.0, identity; q=0.5, *;q=0")
103-
encoding = NegotiateEncoding(req2)
104+
areq2 := AcquireRequest(req2)
105+
encoding = areq2.AcceptEncoding()
106+
assert.True(t, areq2.IsGzipAccepted)
104107
assert.Equal(t, "gzip", encoding.Value)
105108
assert.Equal(t, "gzip;q=1.0", encoding.Raw)
106-
assert.True(t, isGzipAccepted(&Request{}, req1))
107109

108110
req3 := createRawHTTPRequest(HeaderAcceptEncoding, "")
109111
encoding = NegotiateEncoding(req3)
110112
assert.Equal(t, true, encoding == nil)
111113

112114
req4 := createRawHTTPRequest(HeaderAcceptEncoding, "compress;q=0.5")
113-
encoding = NegotiateEncoding(req4)
115+
areq4 := AcquireRequest(req4)
116+
encoding = areq4.AcceptEncoding()
117+
assert.False(t, areq4.IsGzipAccepted)
114118
assert.Equal(t, "compress", encoding.Value)
115119
assert.Equal(t, "compress;q=0.5", encoding.Raw)
116-
assert.False(t, isGzipAccepted(&Request{}, req4))
117120
}
118121

119122
func TestHTTPAcceptHeaderVendorType(t *testing.T) {

0 commit comments

Comments
 (0)