Skip to content

Commit cfd046d

Browse files
committed
render file and bytes. use reply builder
1 parent 8dd7165 commit cfd046d

File tree

4 files changed

+30
-17
lines changed

4 files changed

+30
-17
lines changed

fs.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
)
1212

1313
// FileOnlyFilesystem extends/wraps `http.FileSystem` to disable directory listing
14-
// fucntionality
14+
// functionality
1515
type FileOnlyFilesystem struct {
1616
Fs http.FileSystem
1717
dir string
@@ -36,7 +36,7 @@ func Dir(path string, listDir bool) http.FileSystem {
3636
// FileOnlyFilesystem methods
3737
//___________________________________
3838

39-
// Open method is compilance with `http.FileSystem` interface and disables
39+
// Open method is compliance with `http.FileSystem` interface and disables
4040
// directory listing
4141
func (fs FileOnlyFilesystem) Open(name string) (http.File, error) {
4242
stat, err := os.Lstat(filepath.Join(fs.dir, name))

request.go

+9
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ import (
1313
"aahframework.org/essentials"
1414
)
1515

16+
const jsonpReqParamKey = "callback"
17+
1618
// Request is extends `http.Request` for aah framework
1719
type Request struct {
1820
// Host value of the HTTP 'Host' header (e.g. 'example.com:8080').
@@ -75,6 +77,7 @@ func ParseRequest(r *http.Request, req *Request) *Request {
7577
req.Referer = getReferer(r.Header)
7678
req.ClientIP = ClientIP(r)
7779
req.Locale = NegotiateLocale(r)
80+
req.IsJSONP = isJSONPReqeust(r)
7881
req.Raw = r
7982

8083
return req
@@ -132,3 +135,9 @@ func getReferer(hdr http.Header) string {
132135

133136
return referer
134137
}
138+
139+
func isJSONPReqeust(r *http.Request) bool {
140+
query := r.URL.Query()
141+
callback := query.Get(jsonpReqParamKey)
142+
return !ess.IsStrEmpty(callback)
143+
}

response.go

+7-7
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ type (
3838
}
3939
)
4040

41-
// interface compilance
41+
// interface compliance
4242
var _ http.CloseNotifier = &Response{}
4343
var _ http.Flusher = &Response{}
4444
var _ http.Hijacker = &Response{}
@@ -88,7 +88,7 @@ func (r *Response) Write(buf []byte) (int, error) {
8888
}
8989

9090
// ReadFrom method calls underlying ReadFrom method with given reader if it's
91-
// compatiable and writes HTTP status OK (200) if it's not written yet.
91+
// compatible and writes HTTP status OK (200) if it's not written yet.
9292
func (r *Response) ReadFrom(rdr io.Reader) (int64, error) {
9393
if rf, ok := r.w.(io.ReaderFrom); ok {
9494
r.WriteHeader(http.StatusOK)
@@ -107,7 +107,7 @@ func (r *Response) BytesWritten() int {
107107
// Close method closes the writer if possible.
108108
func (r *Response) Close() {
109109
if c, ok := r.w.(io.Closer); ok {
110-
c.Close()
110+
_ = c.Close()
111111
}
112112
}
113113

@@ -116,26 +116,26 @@ func (r *Response) Unwrap() http.ResponseWriter {
116116
return r.w
117117
}
118118

119-
// CloseNotify method calls underlying CloseNotify method if it's compatiable
119+
// CloseNotify method calls underlying CloseNotify method if it's compatible
120120
func (r *Response) CloseNotify() <-chan bool {
121121
n := r.w.(http.CloseNotifier)
122122
return n.CloseNotify()
123123
}
124124

125-
// Flush method calls underlying Flush method if it's compatiable
125+
// Flush method calls underlying Flush method if it's compatible
126126
func (r *Response) Flush() {
127127
if f, ok := r.w.(http.Flusher); ok {
128128
f.Flush()
129129
}
130130
}
131131

132-
// Hijack method calls underlying Hijack method if it's compatiable otherwise
132+
// Hijack method calls underlying Hijack method if it's compatible otherwise
133133
// returns an error. It becomes the caller's responsibility to manage
134134
// and close the connection.
135135
func (r *Response) Hijack() (net.Conn, *bufio.ReadWriter, error) {
136136
if h, ok := r.w.(http.Hijacker); ok {
137137
return h.Hijack()
138138
}
139139

140-
return nil, nil, errors.New("http.Hijacker interface is not compatiable")
140+
return nil, nil, errors.New("http.Hijacker interface is not compatible")
141141
}

response_test.go

+12-8
Original file line numberDiff line numberDiff line change
@@ -70,21 +70,23 @@ func TestHijackCall(t *testing.T) {
7070

7171
con, rw, err := writer.(http.Hijacker).Hijack()
7272
assert.FailOnError(t, err, "")
73-
defer con.Close()
73+
defer func() {
74+
_ = con.Close()
75+
}()
7476

7577
bytes := []byte("aah framework calling hijack")
76-
rw.WriteString("HTTP/1.1 200 OK\r\n")
77-
rw.WriteString("Date: " + time.Now().Format(http.TimeFormat) + "\r\n")
78-
rw.WriteString("Content-Type: text/plain; charset=utf-8\r\n")
79-
rw.WriteString("Content-Length: " + strconv.Itoa(len(bytes)))
80-
rw.WriteString(string(bytes) + "\r\n")
78+
_, _ = rw.WriteString("HTTP/1.1 200 OK\r\n")
79+
_, _ = rw.WriteString("Date: " + time.Now().Format(http.TimeFormat) + "\r\n")
80+
_, _ = rw.WriteString("Content-Type: text/plain; charset=utf-8\r\n")
81+
_, _ = rw.WriteString("Content-Length: " + strconv.Itoa(len(bytes)))
82+
_, _ = rw.WriteString(string(bytes) + "\r\n")
8183
_ = rw.Flush()
8284
}
8385

8486
server := httptest.NewServer(http.HandlerFunc(handler))
8587
defer server.Close()
8688

87-
http.Get(server.URL)
89+
_, _ = http.Get(server.URL)
8890
}
8991

9092
func TestCallCloseNotifyAndFlush(t *testing.T) {
@@ -110,7 +112,9 @@ func callAndValidate(t *testing.T, handler http.HandlerFunc, response string) {
110112
if err != nil {
111113
t.Fatalf("Error Get: %v", err)
112114
}
113-
defer resp.Body.Close()
115+
defer func() {
116+
_ = resp.Body.Close()
117+
}()
114118

115119
bytes, _ := ioutil.ReadAll(resp.Body)
116120
assert.Equal(t, response, string(bytes))

0 commit comments

Comments
 (0)