From 35b236b4248c40ebe4d94127e6f5a463b27e00c3 Mon Sep 17 00:00:00 2001 From: q191201771 <191201771@qq.com> Date: Sun, 29 Nov 2020 22:22:29 +0800 Subject: [PATCH] =?UTF-8?q?[feat]=20package=20nazahttp:=20=E6=96=B0?= =?UTF-8?q?=E5=A2=9E=E5=87=BD=E6=95=B0ReadHTTPRequest=EF=BC=8C=E8=AF=BB?= =?UTF-8?q?=E5=8F=96HTTP=E8=AF=B7=E6=B1=82=E5=B9=B6=E8=A7=A3=E6=9E=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkg/nazahttp/body.go | 5 ++-- pkg/nazahttp/header.go | 3 --- pkg/nazahttp/http.go | 21 ++++++++++++++++ pkg/nazahttp/post.go | 2 +- pkg/nazahttp/request.go | 53 +++++++++++++++++++++++++++++++++++++++++ 5 files changed, 77 insertions(+), 7 deletions(-) create mode 100644 pkg/nazahttp/http.go create mode 100644 pkg/nazahttp/request.go diff --git a/pkg/nazahttp/body.go b/pkg/nazahttp/body.go index e54a054..7fea735 100644 --- a/pkg/nazahttp/body.go +++ b/pkg/nazahttp/body.go @@ -10,15 +10,14 @@ package nazahttp import ( "encoding/json" - "errors" "io/ioutil" "net/http" "github.com/q191201771/naza/pkg/nazajson" ) -var ErrParamMissing = errors.New("nazahttp: param missing") - +// @brief 从http请求中解析body中的json字符串,并反序列化至结构体中 +// // @param r http请求对象 // @param info 输出参数,用于接收反序列化之后的数据 // @param keyFieldList 可选参数,可指定一个或多个json中必须存在的字段 diff --git a/pkg/nazahttp/header.go b/pkg/nazahttp/header.go index edfcdfd..bbd809b 100644 --- a/pkg/nazahttp/header.go +++ b/pkg/nazahttp/header.go @@ -9,12 +9,9 @@ package nazahttp import ( - "errors" "strings" ) -var ErrHTTPHeader = errors.New("nazahttp: fxxk") - type LineReader interface { ReadLine() (line []byte, isPrefix bool, err error) } diff --git a/pkg/nazahttp/http.go b/pkg/nazahttp/http.go new file mode 100644 index 0000000..5c5025d --- /dev/null +++ b/pkg/nazahttp/http.go @@ -0,0 +1,21 @@ +// Copyright 2020, Chef. All rights reserved. +// https://github.com/q191201771/naza +// +// Use of this source code is governed by a MIT-style license +// that can be found in the License file. +// +// Author: Chef (191201771@qq.com) + +package nazahttp + +import "errors" + +var ( + ErrHTTPHeader = errors.New("nazahttp: fxxk") + ErrParamMissing = errors.New("nazahttp: param missing") +) + +const ( + HeaderFieldContentLength = "Content-Length" + HeaderFieldContentType = "application/json" +) diff --git a/pkg/nazahttp/post.go b/pkg/nazahttp/post.go index acdc42e..4b7375c 100644 --- a/pkg/nazahttp/post.go +++ b/pkg/nazahttp/post.go @@ -26,5 +26,5 @@ func PostJson(url string, info interface{}, client *http.Client) (*http.Response if client == nil { client = http.DefaultClient } - return client.Post(url, "application/json", b) + return client.Post(url, HeaderFieldContentType, b) } diff --git a/pkg/nazahttp/request.go b/pkg/nazahttp/request.go new file mode 100644 index 0000000..47a41e7 --- /dev/null +++ b/pkg/nazahttp/request.go @@ -0,0 +1,53 @@ +// Copyright 2020, Chef. All rights reserved. +// https://github.com/q191201771/naza +// +// Use of this source code is governed by a MIT-style license +// that can be found in the License file. +// +// Author: Chef (191201771@qq.com) + +package nazahttp + +import ( + "io" + "strconv" +) + +// e.g. bufio.Reader +type RequestReader interface { + LineReader + io.Reader +} + +type HTTPRequestCtx struct { + Method string + URI string + Headers map[string]string + Body []byte +} + +// 注意,如果HTTP Header中不包含`Content-Length`,则不会读取HTTP Body,并且err返回值为nil +func ReadHTTPRequest(r RequestReader) (ctx HTTPRequestCtx, err error) { + var requestLine string + requestLine, ctx.Headers, err = ReadHTTPHeader(r) + if err != nil { + return ctx, err + } + ctx.Method, ctx.URI, _, err = ParseHTTPRequestLine(requestLine) + if err != nil { + return ctx, err + } + + contentLength, ok := ctx.Headers[HeaderFieldContentLength] + if !ok { + return ctx, nil + } + cl, err := strconv.Atoi(contentLength) + if err != nil { + return ctx, err + } + ctx.Body = make([]byte, cl) + _, err = io.ReadFull(r, ctx.Body) + + return ctx, err +}