From 2549e70d748c03481ccdfb545b0e129b8f1e5444 Mon Sep 17 00:00:00 2001 From: fangdingjun Date: Wed, 18 Jul 2018 09:30:30 +0800 Subject: [PATCH] fix crash on header callback --- callbacks.go | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/callbacks.go b/callbacks.go index 51a49d3..d09fedc 100644 --- a/callbacks.go +++ b/callbacks.go @@ -183,6 +183,12 @@ func onHeaderCallback(ptr unsafe.Pointer, streamID C.int, //log.Println("onHeaderCallback end") return NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE } + var header http.Header + if conn.isServer { + header = s.request.Header + } else { + header = s.response.Header + } goname = strings.ToLower(goname) switch goname { case ":method": @@ -206,16 +212,24 @@ func onHeaderCallback(ptr unsafe.Pointer, streamID C.int, s.response.StatusCode = statusCode s.response.Status = http.StatusText(statusCode) case "content-length": - s.response.Header.Add(goname, govalue) + header.Add(goname, govalue) n, err := strconv.ParseInt(govalue, 10, 64) if err == nil { - s.response.ContentLength = n + if conn.isServer { + s.request.ContentLength = n + } else { + s.response.ContentLength = n + } } case "transfer-encoding": - s.response.Header.Add(goname, govalue) - s.response.TransferEncoding = append(s.response.TransferEncoding, govalue) + header.Add(goname, govalue) + if conn.isServer { + s.request.TransferEncoding = append(s.response.TransferEncoding, govalue) + } else { + s.response.TransferEncoding = append(s.response.TransferEncoding, govalue) + } default: - s.response.Header.Add(goname, govalue) + header.Add(goname, govalue) } //log.Println("onHeaderCallback end") return NGHTTP2_NO_ERROR