SampleHttp

package module
v0.0.0-...-cc10e7c Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 13, 2022 License: MIT Imports: 9 Imported by: 1

README

SampleHttp

介绍

用于探寻http协议

使用samplehttp
package main

import (
	"fmt"
	"gitee.com/pdudo/SampleHttp"
	"log"
)

func login(info *SampleHttp.HttpInfo) {
	username := info.RequestInfo.RequestData["username"]
	password := info.RequestInfo.RequestData["密码"]

	log.Println(info)

	toClientString := fmt.Sprintf("接收到POST请求参数,username: %s , 密码: %s",username,password)
	info.Write([]byte(toClientString))
}


func main() {

    SampleHttp.Route("post","/login",login)

	// 请请求 /site?名字=1232
    SampleHttp.Route("GET","/site", func(info *SampleHttp.HttpInfo) {
		info.SetHttpStatusCode(200)
		name := info.RequestInfo.RequestData["名字"]
		info.SetResponseHeader("Content-Type","text/html;charset=utf-8")

		log.Println(info)
		toClientString := fmt.Sprintf("路由已经收到参数: name: %s" ,name)
		info.Write([]byte(toClientString))
	})

	SampleHttp.Route("GET","/123", func(info *SampleHttp.HttpInfo) {
		info.SetHttpStatusCode(301)
		info.SetResponseHeader("Location","https://juejin.cn")
	})

	SampleHttp.StartServer("0.0.0.0:8082")
}

Documentation

Index

Constants

View Source
const (
	CRLF  = "\r\n"     // 定义行结束
	CRLF2 = "\r\n\r\n" // 定义首部报文结束符
)

Variables

View Source
var (
	SetResponseHeaderOnce sync.Once
)

Functions

func Route

func Route(methods string, url string, funcName func(info *HttpInfo))

记录路由

func StartServer

func StartServer(addr string)

Types

type HttpInfo

type HttpInfo struct {
	RequestInfo
	ResponseInfo
}

func (*HttpInfo) SetHttpStatusCode

func (info *HttpInfo) SetHttpStatusCode(code uint)

状态码

func (*HttpInfo) SetResponseHeader

func (info *HttpInfo) SetResponseHeader(key string, value string)

func (*HttpInfo) Write

func (info *HttpInfo) Write(msg []byte)

数据

type RequestInfo

type RequestInfo struct {
	Method      string
	Url         string
	HttpVersion string

	RequestHeader map[string]string
	RequestData   map[string]string //接收请求参数
	Body          []byte

	RemoteIP string
}

请求报文

type ResponseInfo

type ResponseInfo struct {
	HttpVersion string
	StatusCode  uint
	StatusMsg   string

	Body []byte
	// contains filtered or unexported fields
}

响应报文

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL