Skip to content

Commit 3fae8cd

Browse files
committed
fix(x/net/http): Fix nil pointer error & optimize naming
Signed-off-by: hackerchai <i@hackerchai.com>
1 parent 0ce31d6 commit 3fae8cd

File tree

3 files changed

+118
-195
lines changed

3 files changed

+118
-195
lines changed

x/net/http/request.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -136,12 +136,12 @@ func (conn *conn) readRequest(hyperReq *hyper.Request) (*Request, error) {
136136
body := hyperReq.Body()
137137
if body != nil {
138138
task := body.Data()
139-
taskID := taskGetBody
139+
taskFlag := getBodyTask
140140
taskData := taskData{
141-
hyperBody: body,
142-
body: nil,
143-
conn: conn,
144-
hyperTaskID: taskID,
141+
hyperBody: body,
142+
responseBody: nil,
143+
conn: conn,
144+
taskFlag: taskFlag,
145145
}
146146
task.SetUserdata(c.Pointer(&taskData), nil)
147147
requestBody := newRequestBody(conn.asyncHandle)

x/net/http/response.go

Lines changed: 21 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"unsafe"
66

77
"github.com/goplus/llgo/c"
8-
"github.com/goplus/llgo/c/libuv"
98
"github.com/goplus/llgo/c/os"
109
"github.com/goplus/llgo/rust/hyper"
1110
)
@@ -17,39 +16,36 @@ type response struct {
1716
body []byte
1817
hyperChannel *hyper.ResponseChannel
1918
hyperResp *hyper.Response
20-
request *Request
21-
asyncHandler *libuv.Async
2219
}
2320

24-
type body struct {
21+
type responseBodyRaw struct {
2522
data []byte
2623
len uintptr
2724
readLen uintptr
2825
}
2926

3027
type taskData struct {
31-
hyperBody *hyper.Body
32-
body *body
33-
conn *conn
34-
hyperTaskID hyperTaskID
28+
hyperBody *hyper.Body
29+
responseBody *responseBodyRaw
30+
conn *conn
31+
taskFlag taskFlag
3532
}
3633

37-
type hyperTaskID int
34+
type taskFlag int
3835

3936
const (
40-
taskSetBody hyperTaskID = iota
41-
taskGetBody
37+
setBodyTask taskFlag = iota
38+
getBodyTask
4239
)
4340

4441
var DefaultChunkSize uintptr = 8192
4542

46-
func newResponse(request *Request, hyperChannel *hyper.ResponseChannel) *response {
43+
func newResponse(hyperChannel *hyper.ResponseChannel) *response {
4744
fmt.Printf("[debug] newResponse called\n")
4845

4946
return &response{
5047
header: make(Header),
5148
hyperChannel: hyperChannel,
52-
//request: request,
5349
statusCode: 200,
5450
written: false,
5551
body: nil,
@@ -65,9 +61,7 @@ func (r *response) Write(data []byte) (int, error) {
6561
if !r.written {
6662
r.WriteHeader(200)
6763
}
68-
fmt.Printf("[debug] data: %s\n", string(data))
6964
r.body = append(r.body, data...)
70-
fmt.Printf("[debug] r.body: %s\n", string(r.body))
7165
return len(data), nil
7266
}
7367

@@ -84,12 +78,12 @@ func (r *response) WriteHeader(statusCode int) {
8478
fmt.Println("[debug] WriteHeaderStatusCode done")
8579

8680
//debug
87-
// fmt.Printf("[debug] < HTTP/1.1 %d\n", statusCode)
88-
// for key, values := range r.header {
89-
// for _, value := range values {
90-
// fmt.Printf("< %s: %s\n", key, value)
91-
// }
92-
// }
81+
fmt.Printf("[debug] < HTTP/1.1 %d\n", statusCode)
82+
for key, values := range r.header {
83+
for _, value := range values {
84+
fmt.Printf("< %s: %s\n", key, value)
85+
}
86+
}
9387

9488
headers := r.hyperResp.Headers()
9589
for key, values := range r.header {
@@ -109,18 +103,11 @@ func (r *response) WriteHeader(statusCode int) {
109103
}
110104
}
111105

112-
fmt.Println("[debug] WriteHeaderHeaders done")
113-
114106
fmt.Println("[debug] WriteHeader done")
115107
}
116108

117109
func (r *response) finalize() error {
118110
fmt.Printf("[debug] finalize called\n")
119-
// err := r.request.Body.Close()
120-
// if err != nil {
121-
// return err
122-
// }
123-
// fmt.Printf("[debug] request body closed\n")
124111

125112
if !r.written {
126113
r.WriteHeader(200)
@@ -132,7 +119,7 @@ func (r *response) finalize() error {
132119
return fmt.Errorf("failed to create response")
133120
}
134121

135-
bodyData := body{
122+
bodyData := responseBodyRaw{
136123
data: r.body,
137124
len: uintptr(len(r.body)),
138125
readLen: 0,
@@ -144,10 +131,10 @@ func (r *response) finalize() error {
144131
return fmt.Errorf("failed to create body")
145132
}
146133
taskData := &taskData{
147-
hyperBody: nil,
148-
body: &bodyData,
149-
conn: nil,
150-
hyperTaskID: taskSetBody,
134+
hyperBody: nil,
135+
responseBody: &bodyData,
136+
conn: nil,
137+
taskFlag: setBodyTask,
151138
}
152139
body.SetDataFunc(setBodyDataFunc)
153140
body.SetUserdata(unsafe.Pointer(taskData), nil)
@@ -173,7 +160,7 @@ func setBodyDataFunc(userdata c.Pointer, ctx *hyper.Context, chunk **hyper.Buf)
173160
fmt.Println("[debug] taskData is nil")
174161
return hyper.PollError
175162
}
176-
body := taskData.body
163+
body := taskData.responseBody
177164

178165
if body.len > 0 {
179166
//debug

0 commit comments

Comments
 (0)