Go 請求和響應--處理請求

前面我們分析了http包中如何根據(jù)請求的URL轉發(fā)請求到對應的處理器處理的,現(xiàn)在我們深入理解處理器是如何解析、處理請求的。

主要內容

  • Request結構
  • 表單
  • 文本處理

1.1 Request結構

Request結構是根據(jù)HTTP請求報文,并按實際情況定義的,除了HTTP請求報文中定義的概念外,還增加了Form字段等信息。以下是Request結構的定義:

type Request struct {
    Method string     // 請求的方法
    URL *url.URL     // 請求報文起始行中的URL,URL類型的指針

    Proto      string    // "HTTP/1.0"
    ProtoMajor int    // 1
    ProtoMinor int    // 0

    Header Header   // 請求頭部字段

    Body io.ReadCloser    // 請求主體

    GetBody func() (io.ReadCloser, error)
    ContentLength int64
    TransferEncoding []string
    Close bool
    Host string
        
    // 請求報文中的一些參數(shù),包括表單字段等
    Form url.Values
    PostForm url.Values
    MultipartForm *multipart.Form

    Trailer Header
    RemoteAddr string
    RequestURI string
    TLS *tls.ConnectionState
    Cancel <-chan struct{}
    Response *Response
    ctx context.Context
}

1.2 URL結構

Request中有個比較重要的字段URL,表示請求行中的URL,定義如下:

type URL struct {
    Scheme     string   // 方案
    Opaque     string    // 
    User       *Userinfo // 基本驗證方式中的username和password信息
    Host       string    // 主機字段
    Path       string    // 路徑
    RawPath    string    // 
    ForceQuery bool      // 
    RawQuery   string    // 查詢字段
    Fragment   string    // 分片字段
}

1.3 請求首部

請求和響應的首部都使用Header類型表示,header類型是一個映射(map)類型,表示HTTP首部中多個鍵值對。

type Header map[string][]string

1.4 請求主體

請求和響應的主體都由Request結構中的Body表示,這個字段是Reader和Closer接口的結合。

我們嘗試一下對Request結構的使用:

package main

import (
    "fmt"
    "net/http"
)

func urlHandler(w http.ResponseWriter, r *http.Request) {
    // Request中的URL結構
    fmt.Fprintln(w, r.URL.Path)
}

func headerHandler(w http.ResponseWriter, r *http.Request) {
    // Request結構中的Header是映射(map)類型,可以通過鍵值對進行操作,也可以使用map實現(xiàn)的Get, Set等方法操作。
    fmt.Fprintln(w, r.Header)
    fmt.Fprintln(w, r.Header.Get("Accept-Encoding"))
}

func bodyHandler(w http.ResponseWriter, r *http.Request) {
    // Request結構中的body字段實現(xiàn)了Reader接口,所以可以使用Read方法。
    len := r.ContentLength
    body := make([]byte, len)
    r.Body.Read(body)
    fmt.Fprintln(w, string(body))
}

func main() {
    http.HandleFunc("/url", urlHandler)
    http.HandleFunc("/header", headerHandler)
    http.HandleFunc("/body", bodyHandler)

    err := http.ListenAndServe(":8000", nil)
    if err != nil {
        fmt.Println(err)
    }
}

2.1 HTML表單

表單是客戶端和服務端進行數(shù)據(jù)交互的載體。通常,表單由POST請求在請求體中傳遞的;當然也可以用GET請求傳遞,但是該表單數(shù)據(jù)將會在URL中也鍵值對的方式傳遞。
表單是一個包含表單元素的區(qū)域,表單元素(文本,下拉列表,單選框,文件等),Go中Request結構定義了Form字段,可以方便對表單進行操作。

<html>
<head>
<title></title>
</head>
<body>
<form action="/login" method="post">
    用戶名:<input type="text" name="username">
    密碼:<input type="password" name="password">
    <input type="submit" value="登錄">
</form>
</body>
</html>
package main

import (
    "fmt"
    "html/template"
    "net/http"
)

func login(w http.ResponseWriter, r *http.Request) {
    // 登錄邏輯,GET方法時放回登錄頁面,POST解析用戶、密碼進行登錄驗證。
    r.ParseForm() // 解析url傳遞的參數(shù),對于POST請求則解析請求體(request body)
    if r.Method == "POST" {
        fmt.Println(r.Form["username"])
        fmt.Println(r.Form["password"])
    } else {
        t, _ := template.ParseFiles("login.html")
        t.Execute(w, nil)
    }
}

func main() {
    http.HandleFunc("/login", login)

    err := http.ListenAndServe(":8000", nil)
    if err != nil {
        fmt.Println(err)
    }
}

2.2 文件

Web用戶經(jīng)常會使用文件上傳功能,比如上傳頭像圖片等。我們來實現(xiàn)文件上傳功能。
首先我們先了解一下form的enctype屬性,它決定了請求時在發(fā)送鍵值對將會使用的格式。enctype的三種格式:

格式 說明
application/x-www-form-urlencoded form的默認值,使用此格式的時候,表單中的數(shù)據(jù)被編碼成一個連續(xù)的“長查詢字符串”,不同的鍵值對將使用&符號隔開,而鍵值之間使用等號(=)隔開。這種形式是跟URL編碼一樣的。如:username=admin&password=123
multipart/form-data 將數(shù)據(jù)編碼成MIME報文,表單中的每個鍵值對都帶有各自的內容類型和內容配置(disposition)
text/plain 空格轉換為 "+" 加號,但不對特殊字符編碼。

multipart/form-data格式常用于文件上傳功能,所以我們以這種格式來示范文件上傳功能。

<html>
<head>
<title></title>
</head>
<body>
<form action="/upload" method="post" enctype="multipart/form-data">
    <input type="file" name="uploadfile" />
    <input type="submit" value="upload">
</form>
</body>
</html>
package main

import (
    "fmt"
    "html/template"
    "io"
    "net/http"
    "os"
)

func upload(w http.ResponseWriter, r *http.Request) {
    if r.Method == "POST" {
        //
        r.ParseMultipartForm(1024)
        // 使用r.FormFile獲取文件句柄,然后對文件進行存儲等處理。
        file, handler, err := r.FormFile("uploadfile")
        defer file.Close()

        if err != nil {
            fmt.Println(err)
            return
        }

        fmt.Fprintf(w, "%v", handler.Header)

        // 假設已經(jīng)有upload目錄,存儲文件。
        f, err := os.OpenFile("../static/upload/"+handler.Filename, os.O_WRONLY|os.O_CREATE, 0666)
        if err != nil {
            fmt.Println(err)
            return
        }
        defer f.Close()
        io.Copy(f, file)
    } else {
        t, _ := template.ParseFiles("upload.html")
        t.Execute(w, nil)
    }

}

func main() {
    http.HandleFunc("/upload", upload)
    err := http.ListenAndServe(":8000", nil)
    if err != nil {
        fmt.Println(err)
    }
}

本節(jié)詳細說明了Go中的Request結構,以及結合表單的使用。接下來介紹cookie與session的使用。

代碼傳送門

?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容