12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034 |
- //go:build windows
- package yu_hpsocket
- import (
- yu_http "gogs.qqck.cn/s/tools/http"
- yu_strings "gogs.qqck.cn/s/tools/strings"
- yu_sys "gogs.qqck.cn/s/tools/sys"
- "unsafe"
- )
- // HttpServer
- // @Description: HttpServerListener 对象,请自己创建和销毁,目的是保证安全
- type HttpServer struct {
- Server[HttpServer]
- TcpServer
- // 事件----------------------------------
- onMessageBegin func(t *HttpServer, Sender, ConnID uintptr) HPR
- onRequestLine func(t *HttpServer, Sender, ConnID uintptr, Method, Url string) HPR
- onHeader func(t *HttpServer, Sender, ConnID uintptr, Name, Value string) HPR
- onHeadersComplete func(t *HttpServer, Sender, ConnID uintptr) HPR
- onBody func(t *HttpServer, Sender, ConnID, Data uintptr, Length uintptr) HPR
- onChunkHeader func(t *HttpServer, Sender, ConnID uintptr, Length uintptr) HPR
- onChunkComplete func(t *HttpServer, Sender, ConnID uintptr) HPR
- onMessageComplete func(t *HttpServer, Sender, ConnID uintptr) HPR
- onUpgrade func(t *HttpServer, Sender, ConnID uintptr, UpgradeType HUT) HPR
- onParseError func(t *HttpServer, Sender, ConnID uintptr, ErrorCode int, ErrorDesc string) HPR
- onWSMessageHeader func(t *HttpServer, Sender, ConnID uintptr, Final bool, Reserved, OperationCode byte, Mask []byte, BodyLen int64) HR
- onWSMessageBody func(t *HttpServer, Sender, ConnID, Data uintptr, Length uintptr) HR
- onWSMessageComplete func(t *HttpServer, Sender, ConnID uintptr) HR
- }
- // NewHttpServer
- //
- // @Description: 创建 HttpServer 对象
- func NewHttpServer() (t *HttpServer) {
- t = &HttpServer{}
- return
- }
- // Create
- //
- // @Description: 创建 HttpServer 环境
- func (t *HttpServer) Create(SSL bool) bool {
- t.Close()
- if t.hListener, _, _ = Create_HP_HttpServerListener.Call(); t.hListener == 0 {
- return false
- }
- if t.SSL = SSL; t.SSL {
- t.hServer, _, _ = Create_HP_HttpsServer.Call(t.hListener)
- } else {
- t.hServer, _, _ = Create_HP_HttpServer.Call(t.hListener)
- }
- if t.hServer == 0 {
- Destroy_HP_HttpServerListener.Call(t.hListener)
- t.hListener = 0
- return false
- }
- _server_on_set(t.hServer, &_server_item{
- _type: _server_type_HttpServer,
- t: t,
- })
- HP_Set_FN_HttpServer_OnHeader.Call(t.hListener, server_http_callback_OnHeader_ptr) // 默认绑定,OnHeader 有问题,会内存溢出
- return true
- }
- // Close
- //
- // @Description: 销毁 HttpServer 环境
- func (t *HttpServer) Close() {
- t.Stop()
- t.Wait(-1)
- if t.hServer != 0 {
- if t.SSL {
- Destroy_HP_HttpsServer.Call(t.hServer)
- } else {
- Destroy_HP_HttpServer.Call(t.hServer)
- }
- _server_on_del(t.hServer)
- t.hServer = 0
- }
- if t.hListener != 0 {
- Destroy_HP_HttpServerListener.Call(t.hListener)
- t.hListener = 0
- }
- }
- // SendResponse
- //
- // @Description: 向客户端回复 HTTP 请求
- // @param ConnID 连接 ID
- // @param StatusCode HTTP 状态码
- // @param Desc HTTP 状态描述,空为默认
- // @param Headers 回复请求头
- // @param Data 回复请求体
- func (t *HttpServer) SendResponse(ConnID uintptr, StatusCode yu_http.Status, Desc string, Headers []TNVPair, Data []byte) bool {
- if !t.isValid() {
- return false
- }
- if Desc == "" {
- Desc = yu_http.StatusText(StatusCode)
- }
- j_headers_buf := make([][2][]byte, len(Headers))
- j_headers := make([][2]unsafe.Pointer, len(Headers))
- var j_headers_ptr unsafe.Pointer
- if len(Headers) > 0 {
- for j_i := 0; j_i < len(Headers); j_i++ {
- j_headers_buf[j_i][0] = yu_strings.Utf8ToGbkBytes(Headers[j_i].Name)
- j_headers[j_i][0] = unsafe.Pointer(&j_headers_buf[j_i][0][0])
- j_headers_buf[j_i][1] = yu_strings.Utf8ToGbkBytes(Headers[j_i].Value)
- j_headers[j_i][1] = unsafe.Pointer(&j_headers_buf[j_i][1][0])
- }
- j_headers_ptr = unsafe.Pointer(&j_headers[0])
- }
- return HP_HttpServer_SendResponse.CallBool(t.hServer, ConnID,
- uintptr(StatusCode), yu_sys.S{yu_sys.Gbk, Desc},
- j_headers_ptr, len(Headers), Data, len(Data),
- )
- }
- // SendResponseS
- //
- // @Description: 向客户端回复 HTTP 请求
- // @param ConnID 连接 ID
- // @param StatusCode HTTP 状态码
- // @param Desc HTTP 状态描述,空为默认
- // @param Headers 回复请求头
- // @param Data 回复请求体
- func (t *HttpServer) SendResponseS(ConnID uintptr, StatusCode yu_http.Status, Desc string, Headers []TNVPair, Data string) bool {
- if !t.isValid() {
- return false
- }
- if Desc == "" {
- Desc = yu_http.StatusText(StatusCode)
- }
- j_headers_buf := make([][2][]byte, len(Headers))
- j_headers := make([][2]unsafe.Pointer, len(Headers))
- var j_headers_ptr unsafe.Pointer
- if len(Headers) > 0 {
- for j_i := 0; j_i < len(Headers); j_i++ {
- j_headers_buf[j_i][0] = yu_strings.Utf8ToGbkBytes(Headers[j_i].Name)
- j_headers[j_i][0] = unsafe.Pointer(&j_headers_buf[j_i][0][0])
- j_headers_buf[j_i][1] = yu_strings.Utf8ToGbkBytes(Headers[j_i].Value)
- j_headers[j_i][1] = unsafe.Pointer(&j_headers_buf[j_i][1][0])
- }
- j_headers_ptr = unsafe.Pointer(&j_headers[0])
- }
- return HP_HttpServer_SendResponse.CallBool(t.hServer, ConnID,
- uintptr(StatusCode), yu_sys.S{yu_sys.Gbk, Desc},
- j_headers_ptr, len(Headers), Data, len(Data),
- )
- }
- // SendChunkData
- //
- // @Description: 向对端发送 Chunked 数据分片
- // @param ConnID 连接 ID
- // @param Data Chunked 数据分片(为 nil 或 len(data)=0 表示结束分片)
- // @param Extensions 扩展属性
- func (t *HttpServer) SendChunkData(ConnID uintptr, Data []byte, Extensions int) bool {
- if !t.isValid() {
- return false
- }
- return HP_HttpServer_SendChunkData.CallBool(t.hServer, ConnID, Data, len(Data), Extensions)
- }
- // SendChunkDataS
- //
- // @Description: 向对端发送 Chunked 数据分片
- // @param ConnID 连接 ID
- // @param Data Chunked 数据分片(为 nil 或 len(data)=0 表示结束分片)
- // @param Extensions 扩展属性
- func (t *HttpServer) SendChunkDataS(ConnID uintptr, Data string, Extensions int) bool {
- if !t.isValid() {
- return false
- }
- return HP_HttpServer_SendChunkData.CallBool(t.hServer, ConnID, Data, len(Data), Extensions)
- }
- // SendLocalFile
- //
- // @Description: 向指定连接发送 4096 KB 以下的小文件
- // @param ConnID 连接 ID
- // @param FileName 文件路径
- // @param StatusCode HTTP 状态码
- // @param Desc HTTP 状态描述,空为默认
- // @param Headers 回复请求头
- func (t *HttpServer) SendLocalFile(ConnID uintptr, FileName string, StatusCode yu_http.Status, Desc string, Headers []TNVPair) bool {
- if !t.isValid() {
- return false
- }
- if Desc == "" {
- Desc = yu_http.StatusText(StatusCode)
- }
- j_headers_buf := make([][2][]byte, len(Headers))
- j_headers := make([][2]unsafe.Pointer, len(Headers))
- var j_headers_ptr unsafe.Pointer
- if len(Headers) > 0 {
- for j_i := 0; j_i < len(Headers); j_i++ {
- j_headers_buf[j_i][0] = yu_strings.Utf8ToGbkBytes(Headers[j_i].Name)
- j_headers[j_i][0] = unsafe.Pointer(&j_headers_buf[j_i][0][0])
- j_headers_buf[j_i][1] = yu_strings.Utf8ToGbkBytes(Headers[j_i].Value)
- j_headers[j_i][1] = unsafe.Pointer(&j_headers_buf[j_i][1][0])
- }
- j_headers_ptr = unsafe.Pointer(&j_headers[0])
- }
- return HP_HttpServer_SendLocalFile.CallBool(t.hServer, ConnID,
- yu_sys.S{yu_sys.Gbk, FileName},
- uintptr(StatusCode), yu_sys.S{yu_sys.Gbk, Desc},
- j_headers_ptr, len(Headers),
- )
- }
- // SendWSMessage
- //
- // @Description: 向对端端发送 WebSocket 消息
- // @param ConnID 连接 ID
- // @param Final 是否结束帧
- // @param Reserved RSV1/RSV2/RSV3 各 1 位
- // @param OperationCode 操作码:0x0 - 0xF
- // @param Data 消息体数据缓冲区
- // @param BodyLen 消息总长度
- func (t *HttpServer) SendWSMessage(ConnID uintptr, Final bool, Reserved, OperationCode byte, Data []byte, BodyLen int64) bool {
- if !t.isValid() {
- return false
- }
- return HP_HttpServer_SendWSMessage.CallBool(t.hServer, ConnID, Final, Reserved, OperationCode, Data, len(Data), BodyLen)
- }
- // SendWSMessageS
- //
- // @Description: 向对端端发送 WebSocket 消息
- // @param ConnID 连接 ID
- // @param Final 是否结束帧
- // @param Reserved RSV1/RSV2/RSV3 各 1 位
- // @param OperationCode 操作码:0x0 - 0xF
- // @param Data 消息体数据缓冲区
- // @param BodyLen 消息总长度
- func (t *HttpServer) SendWSMessageS(ConnID uintptr, Final bool, Reserved, OperationCode byte, Data string, BodyLen int64) bool {
- if !t.isValid() {
- return false
- }
- return HP_HttpServer_SendWSMessage.CallBool(t.hServer, ConnID, Final, Reserved, OperationCode, Data, len(Data), BodyLen)
- }
- // Release
- //
- // @Description: 把连接放入释放队列,等待某个时间(通过 SetReleaseDelay() 设置)关闭连接
- // @param ConnID 连接 ID
- func (t *HttpServer) Release(ConnID uintptr) bool {
- if !t.isValid() {
- return false
- }
- return HP_HttpServer_Release.CallBool(t.hServer, ConnID)
- }
- // SetReleaseDelay
- //
- // @Description: 设置连接释放延时(默认:3000 毫秒)
- func (t *HttpServer) SetReleaseDelay(ReleaseDelay int) *HttpServer {
- if !t.isValid() {
- return t
- }
- HP_HttpServer_SetReleaseDelay.Call(t.hServer, ReleaseDelay)
- return t
- }
- // GetReleaseDelay
- //
- // @Description: 获取连接释放延时
- func (t *HttpServer) GetReleaseDelay() int {
- if !t.isValid() {
- return 0
- }
- return HP_HttpServer_GetReleaseDelay.CallInt(t.hServer)
- }
- // GetUrlFieldSet
- //
- // @Description: 获取请求行 URL 域掩码(URL 域参考:EnHttpUrlField)
- // @param ConnID 连接 ID
- func (t *HttpServer) GetUrlFieldSet(ConnID uintptr) HUF {
- if !t.isValid() {
- return 0
- }
- return HUF(HP_HttpServer_GetUrlFieldSet.CallUintptr(t.hServer, ConnID))
- }
- // GetUrlField
- //
- // @Description: 获取某个 URL 域值
- // @param ConnID 连接 ID
- // @param Field HUF_ 开头常量
- func (t *HttpServer) GetUrlField(ConnID uintptr, Field HUF) string {
- if !t.isValid() {
- return ""
- }
- j_value := HP_HttpServer_GetUrlField.CallGbkToUtf8(t.hServer, ConnID, uintptr(Field))
- if Field == HUF_PATH {
- if j_value == "" {
- j_value = "/"
- } else if j_value[len(j_value)-1] != '/' {
- j_value += "/"
- } else {
- for j_i := len(j_value) - 1; j_i > -1; j_i-- {
- if j_value[j_i] == '/' {
- j_value = j_value[:j_i+1]
- } else {
- break
- }
- }
- }
- }
- return j_value
- }
- // GetMethod
- //
- // @Description: 获取请求方法
- // @param ConnID v
- func (t *HttpServer) GetMethod(ConnID uintptr) string {
- if !t.isValid() {
- return ""
- }
- return HP_HttpServer_GetMethod.CallGbkToUtf8(t.hServer, ConnID)
- }
- // SetLocalVersion
- //
- // @Description: 设置本地协议版本
- func (t *HttpServer) SetLocalVersion(Version HV) *HttpServer {
- if !t.isValid() {
- return t
- }
- HP_HttpServer_SetLocalVersion.Call(t.hServer, uintptr(Version))
- return t
- }
- // GetLocalVersion
- //
- // @Description: 获取本地协议版本
- func (t *HttpServer) GetLocalVersion() HV {
- if !t.isValid() {
- return 0
- }
- return HV(HP_HttpServer_GetLocalVersion.CallUintptr(t.hServer))
- }
- // IsUpgrade
- //
- // @Description: 检查是否升级协议
- // @param ConnID 连接 ID
- func (t *HttpServer) IsUpgrade(ConnID uintptr) bool {
- if !t.isValid() {
- return false
- }
- return HP_HttpServer_IsUpgrade.CallBool(t.hServer, ConnID)
- }
- // IsKeepAlive
- //
- // @Description: 检查是否有 Keep-Alive 标识
- // @param ConnID 连接 ID
- func (t *HttpServer) IsKeepAlive(ConnID uintptr) bool {
- if !t.isValid() {
- return false
- }
- return HP_HttpServer_IsKeepAlive.CallBool(t.hServer, ConnID)
- }
- // StartHttp
- //
- // @Description: 启动 HTTP 通信,当通信组件设置为非自动启动 HTTP 通信时,需要调用本方法启动 HTTP 通信
- // @param ConnID 连接 ID
- func (t *HttpServer) StartHttp(ConnID uintptr) bool {
- if !t.isValid() {
- return false
- }
- return HP_HttpServer_StartHttp.CallBool(t.hServer, ConnID)
- }
- // SetHttpAutoStart
- //
- // @Description: 设置 HTTP 启动方式(默认:TRUE,自动启动)
- func (t *HttpServer) SetHttpAutoStart(AutoStart bool) *HttpServer {
- if !t.isValid() {
- return t
- }
- HP_HttpServer_SetHttpAutoStart.Call(t.hServer, AutoStart)
- return t
- }
- // IsHttpAutoStart
- //
- // @Description: 获取 HTTP 启动方式
- func (t *HttpServer) IsHttpAutoStart() bool {
- if !t.isValid() {
- return false
- }
- return HP_HttpServer_IsHttpAutoStart.CallBool(t.hServer)
- }
- // GetVersion
- //
- // @Description: 获取协议版本
- // @param ConnID ConnID 连接 ID
- func (t *HttpServer) GetVersion(ConnID uintptr) HV {
- if !t.isValid() {
- return 0
- }
- return HV(HP_HttpServer_GetVersion.CallUintptr(t.hServer, ConnID))
- }
- // GetHost
- //
- // @Description: 获取主机
- // @param ConnID 连接 ID
- func (t *HttpServer) GetHost(ConnID uintptr) string {
- if !t.isValid() {
- return ""
- }
- return HP_HttpServer_GetHost.CallGbkToUtf8(t.hServer, ConnID)
- }
- // GetContentLength
- //
- // @Description: 获取内容长度
- // @param ConnID 连接 ID
- func (t *HttpServer) GetContentLength(ConnID uintptr) int64 {
- if !t.isValid() {
- return 0
- }
- return HP_HttpServer_GetContentLength.CallInt64(t.hServer, ConnID)
- }
- // GetContentType
- //
- // @Description: 获取内容类型
- // @param ConnID 连接 ID
- func (t *HttpServer) GetContentType(ConnID uintptr) string {
- if !t.isValid() {
- return ""
- }
- return HP_HttpServer_GetContentType.CallGbkToUtf8(t.hServer, ConnID)
- }
- // GetContentEncoding
- //
- // @Description: 获取内容编码
- // @param ConnID 连接 ID
- func (t *HttpServer) GetContentEncoding(ConnID uintptr) string {
- if !t.isValid() {
- return ""
- }
- return HP_HttpServer_GetContentEncoding.CallGbkToUtf8(t.hServer, ConnID)
- }
- // GetTransferEncoding
- //
- // @Description: 获取传输编码
- // @param ConnID 连接 ID
- func (t *HttpServer) GetTransferEncoding(ConnID uintptr) string {
- if !t.isValid() {
- return ""
- }
- return HP_HttpServer_GetTransferEncoding.CallGbkToUtf8(t.hServer, ConnID)
- }
- // GetUpgradeType
- //
- // @Description: 获取协议升级类型
- // @param ConnID 连接 ID
- func (t *HttpServer) GetUpgradeType(ConnID uintptr) HUT {
- if !t.isValid() {
- return 0
- }
- return HUT(HP_HttpServer_GetUpgradeType.CallInt(t.hServer, ConnID))
- }
- // GetParseErrorCode
- //
- // @Description: 获取解析错误代码
- // @param ConnID 连接 ID
- // @return ErrorCode
- // @return ErrorDesc
- func (t *HttpServer) GetParseErrorCode(ConnID uintptr) (ErrorCode int, ErrorDesc string) {
- if !t.isValid() {
- return
- }
- var j_ErrorDesc uintptr
- ErrorCode = HP_HttpServer_GetParseErrorCode.CallInt(t.hServer, ConnID, &j_ErrorDesc)
- ErrorDesc = yu_strings.GbkPtrToUtf8(j_ErrorDesc)
- return
- }
- // GetHeader
- //
- // @Description: 获取某个请求头(单值)
- // @param ConnID 连接 ID
- func (t *HttpServer) GetHeader(ConnID uintptr, Name string) string {
- if !t.isValid() {
- return ""
- }
- var j_value uintptr
- if !HP_HttpServer_GetHeader.CallBool(t.hServer, ConnID,
- yu_sys.S{yu_sys.Gbk, Name}, &j_value,
- ) {
- return ""
- }
- return yu_strings.GbkPtrToUtf8(j_value)
- }
- // GetHeaders
- //
- // @Description: 获取某个请求头(多值)
- // @param ConnID 连接 ID
- func (t *HttpServer) GetHeaders(ConnID uintptr, Name string) (values []string) {
- if !t.isValid() {
- return
- }
- var j_count int
- HP_HttpServer_GetHeaders.Call(t.hServer, ConnID, yu_sys.S{yu_sys.Gbk, Name}, 0, &j_count)
- if j_count == 0 {
- return
- }
- j_values := make([]uintptr, j_count)
- if !HP_HttpServer_GetHeaders.CallBool(t.hServer, ConnID, yu_sys.S{yu_sys.Gbk, Name}, j_values, &j_count) {
- return
- }
- values = make([]string, 0, j_count)
- for _, j_value := range j_values {
- values = append(values, yu_strings.GbkPtrToUtf8(j_value))
- }
- return
- }
- // GetAllHeaders
- //
- // @Description: 取得所有HTTP返回协议头
- // @param ConnID 连接 ID
- func (t *HttpServer) GetAllHeaders(ConnID uintptr) (values []TNVPair) {
- if !t.isValid() {
- return
- }
- var j_count int
- HP_HttpServer_GetAllHeaders.Call(t.hServer, ConnID, 0, &j_count)
- if j_count == 0 {
- return
- }
- j_values := make([][2]uintptr, j_count)
- if !HP_HttpServer_GetAllHeaders.CallBool(t.hServer, ConnID, unsafe.Pointer(&j_values[0]), &j_count) {
- return
- }
- values = make([]TNVPair, j_count)
- for j_i := 0; j_i < j_count; j_i++ {
- values[j_i].Name = yu_strings.GbkPtrToUtf8(j_values[j_i][0])
- values[j_i].Value = yu_strings.GbkPtrToUtf8(j_values[j_i][1])
- }
- return
- }
- // GetAllHeaderNames
- //
- // @Description: 获取所有请求头名称
- // @param ConnID 连接 ID
- func (t *HttpServer) GetAllHeaderNames(ConnID uintptr) (values []string) {
- if !t.isValid() {
- return
- }
- var j_count int
- HP_HttpServer_GetAllHeaderNames.Call(t.hServer, ConnID, 0, &j_count)
- if j_count == 0 {
- return
- }
- j_values := make([]uintptr, j_count)
- if !HP_HttpServer_GetAllHeaderNames.CallBool(t.hServer, ConnID, j_values, &j_count) {
- return
- }
- values = make([]string, 0, j_count)
- for _, j_value := range j_values {
- values = append(values, yu_strings.GbkPtrToUtf8(j_value))
- }
- return
- }
- // GetCookie
- //
- // @Description: 获取 Cookie
- // @param ConnID 连接 ID
- func (t *HttpServer) GetCookie(ConnID uintptr, Name string) string {
- if !t.isValid() {
- return ""
- }
- var j_value uintptr
- if !HP_HttpServer_GetCookie.CallBool(t.hServer, ConnID,
- yu_sys.S{yu_sys.Gbk, Name}, &j_value,
- ) {
- return ""
- }
- return yu_strings.GbkPtrToUtf8(j_value)
- }
- // GetAllCookies
- //
- // @Description: 获取所有 Cookie
- // @param ConnID 连接 ID
- func (t *HttpServer) GetAllCookies(ConnID uintptr) (values []TNVPair) {
- if !t.isValid() {
- return
- }
- var j_count int
- HP_HttpServer_GetAllCookies.Call(t.hServer, ConnID, 0, &j_count)
- if j_count == 0 {
- return
- }
- j_values := make([][2]uintptr, j_count)
- if !HP_HttpServer_GetAllCookies.CallBool(t.hServer, ConnID, unsafe.Pointer(&j_values[0]), &j_count) {
- return
- }
- values = make([]TNVPair, j_count)
- for j_i := 0; j_i < j_count; j_i++ {
- values[j_i].Name = yu_strings.GbkPtrToUtf8(j_values[j_i][0])
- values[j_i].Value = yu_strings.GbkPtrToUtf8(j_values[j_i][1])
- }
- return
- }
- // GetWSMessageState
- //
- // @Description: 获取当前 WebSocket 消息状态
- // @param ConnID 连接 ID
- func (t *HttpServer) GetWSMessageState(ConnID uintptr) (Final bool, Reserved, OperationCode byte, Mask [4]byte, BodyLen int64, BodyRemain int64) {
- var j_Mask uintptr
- if !HP_HttpServer_GetWSMessageState.CallBool(t.hServer, ConnID, &Final, &Reserved, &OperationCode, &j_Mask, &BodyLen, &BodyRemain) {
- return
- }
- *((*uintptr)(unsafe.Pointer(&Mask))) = j_Mask
- return
- }
- // OnMessageBegin
- //
- // @Description: 【可选】绑定开始解析事件
- // @param call->return HPR_ 开头常量
- func (t *HttpServer) OnMessageBegin(call func(t *HttpServer, Sender, ConnID uintptr) HPR) *HttpServer {
- if !t.isValid() {
- return t
- }
- if call == nil {
- HP_Set_FN_HttpServer_OnMessageBegin.Call(t.hListener, 0)
- } else {
- t.onMessageBegin = call
- HP_Set_FN_HttpServer_OnMessageBegin.Call(t.hListener, server_http_callback_OnMessageBegin_ptr)
- }
- return t
- }
- // OnRequestLine
- //
- // @Description: 【可选】绑定请求行解析完成事件
- // @param call->return HPR_ 开头常量
- func (t *HttpServer) OnRequestLine(call func(t *HttpServer, Sender, ConnID uintptr, Method, Url string) HPR) *HttpServer {
- if !t.isValid() {
- return t
- }
- if call == nil {
- HP_Set_FN_HttpServer_OnRequestLine.Call(t.hListener, 0)
- } else {
- t.onRequestLine = call
- HP_Set_FN_HttpServer_OnRequestLine.Call(t.hListener, server_http_callback_OnRequestLine_ptr)
- }
- return t
- }
- // _OnHeader
- //
- // @Description: 【可选】绑定请求头事件
- // @param call->return HPR_ 开头常量
- func (t *HttpServer) _OnHeader(call func(t *HttpServer, Sender, ConnID uintptr, Name, Value string) HPR) *HttpServer {
- if !t.isValid() {
- return t
- }
- if call == nil {
- HP_Set_FN_HttpServer_OnHeader.Call(t.hListener, 0)
- } else {
- t.onHeader = call
- HP_Set_FN_HttpServer_OnHeader.Call(t.hListener, server_http_callback_OnHeader_ptr)
- }
- return t
- }
- // OnHeadersComplete
- //
- // @Description: 【必选】绑定请求头完成事件
- // @param call->return HPR_ 开头常量
- func (t *HttpServer) OnHeadersComplete(call func(t *HttpServer, Sender, ConnID uintptr) HPR) *HttpServer {
- if !t.isValid() {
- return t
- }
- if call == nil {
- HP_Set_FN_HttpServer_OnHeadersComplete.Call(t.hListener, 0)
- } else {
- t.onHeadersComplete = call
- HP_Set_FN_HttpServer_OnHeadersComplete.Call(t.hListener, server_http_callback_OnHeadersComplete_ptr)
- }
- return t
- }
- // OnBody
- //
- // @Description: 【必选】绑定请求体报文事件
- // @param call->return HPR_ 开头常量
- func (t *HttpServer) OnBody(call func(t *HttpServer, Sender, ConnID, Data uintptr, Length uintptr) HPR) *HttpServer {
- if !t.isValid() {
- return t
- }
- if call == nil {
- HP_Set_FN_HttpServer_OnBody.Call(t.hListener, 0)
- } else {
- t.onBody = call
- HP_Set_FN_HttpServer_OnBody.Call(t.hListener, server_http_callback_OnBody_ptr)
- }
- return t
- }
- // OnChunkHeader
- //
- // @Description: 【可选】绑定Chunked 报文头事件
- // @param call->return HPR_ 开头常量
- func (t *HttpServer) OnChunkHeader(call func(t *HttpServer, Sender, ConnID uintptr, Length uintptr) HPR) *HttpServer {
- if !t.isValid() {
- return t
- }
- if call == nil {
- HP_Set_FN_HttpServer_OnChunkHeader.Call(t.hListener, 0)
- } else {
- t.onChunkHeader = call
- HP_Set_FN_HttpServer_OnChunkHeader.Call(t.hListener, server_http_callback_OnChunkHeader_ptr)
- }
- return t
- }
- // OnChunkComplete
- //
- // @Description: 【可选】绑定Chunked 报文完成事件
- // @param call->return HPR_ 开头常量
- func (t *HttpServer) OnChunkComplete(call func(t *HttpServer, Sender, ConnID uintptr) HPR) *HttpServer {
- if !t.isValid() {
- return t
- }
- if call == nil {
- HP_Set_FN_HttpServer_OnChunkComplete.Call(t.hListener, 0)
- } else {
- t.onChunkComplete = call
- HP_Set_FN_HttpServer_OnChunkComplete.Call(t.hListener, server_http_callback_OnChunkComplete_ptr)
- }
- return t
- }
- // OnMessageComplete
- //
- // @Description: 【必选】绑定完成解析事件
- // @param call->return HPR_ 开头常量
- func (t *HttpServer) OnMessageComplete(call func(t *HttpServer, Sender, ConnID uintptr) HPR) *HttpServer {
- if !t.isValid() {
- return t
- }
- if call == nil {
- HP_Set_FN_HttpServer_OnMessageComplete.Call(t.hListener, 0)
- } else {
- t.onMessageComplete = call
- HP_Set_FN_HttpServer_OnMessageComplete.Call(t.hListener, server_http_callback_OnMessageComplete_ptr)
- }
- return t
- }
- // OnUpgrade
- //
- // @Description: 【可选】绑定升级协议事件
- // @param call->return HPR_ 开头常量
- func (t *HttpServer) OnUpgrade(call func(t *HttpServer, Sender, ConnID uintptr, UpgradeType HUT) HPR) *HttpServer {
- if !t.isValid() {
- return t
- }
- if call == nil {
- HP_Set_FN_HttpServer_OnUpgrade.Call(t.hListener, 0)
- } else {
- t.onUpgrade = call
- HP_Set_FN_HttpServer_OnUpgrade.Call(t.hListener, server_http_callback_OnUpgrade_ptr)
- }
- return t
- }
- // OnParseError
- //
- // @Description: 【必选】绑定解析错误事件
- // @param call->return HPR_ 开头常量
- func (t *HttpServer) OnParseError(call func(t *HttpServer, Sender, ConnID uintptr, ErrorCode int, ErrorDesc string) HPR) *HttpServer {
- if !t.isValid() {
- return t
- }
- if call == nil {
- HP_Set_FN_HttpServer_OnParseError.Call(t.hListener, 0)
- } else {
- t.onParseError = call
- HP_Set_FN_HttpServer_OnParseError.Call(t.hListener, server_http_callback_OnParseError_ptr)
- }
- return t
- }
- // OnWSMessageHeader
- //
- // @Description: 【可选】绑定WebSocket 数据包头事件
- // @param call->return HR_ 开头常量
- func (t *HttpServer) OnWSMessageHeader(call func(t *HttpServer, Sender, ConnID uintptr, Final bool, Reserved, OperationCode byte, Mask []byte, BodyLen int64) HR) *HttpServer {
- if !t.isValid() {
- return t
- }
- if call == nil {
- HP_Set_FN_HttpServer_OnWSMessageHeader.Call(t.hListener, 0)
- } else {
- t.onWSMessageHeader = call
- HP_Set_FN_HttpServer_OnWSMessageHeader.Call(t.hListener, server_http_callback_OnWSMessageHeader_ptr)
- }
- return t
- }
- // OnWSMessageBody
- //
- // @Description: 【可选】绑定WebSocket 数据包体事件
- // @param call->return HR_ 开头常量
- func (t *HttpServer) OnWSMessageBody(call func(t *HttpServer, Sender, ConnID, Data uintptr, Length uintptr) HR) *HttpServer {
- if !t.isValid() {
- return t
- }
- if call == nil {
- HP_Set_FN_HttpServer_OnWSMessageBody.Call(t.hListener, 0)
- } else {
- t.onWSMessageBody = call
- HP_Set_FN_HttpServer_OnWSMessageBody.Call(t.hListener, server_http_callback_OnWSMessageBody_ptr)
- }
- return t
- }
- // OnWSMessageComplete
- //
- // @Description: 【可选】绑定WebSocket 数据包完成事件
- // @param call->return HR_ 开头常量
- func (t *HttpServer) OnWSMessageComplete(call func(t *HttpServer, Sender, ConnID uintptr) HR) *HttpServer {
- if !t.isValid() {
- return t
- }
- if call == nil {
- HP_Set_FN_HttpServer_OnWSMessageComplete.Call(t.hListener, 0)
- } else {
- t.onWSMessageComplete = call
- HP_Set_FN_HttpServer_OnWSMessageComplete.Call(t.hListener, server_http_callback_OnWSMessageComplete_ptr)
- }
- return t
- }
- // OnPrepareListen
- //
- // @Description: 【可选】绑定准备监听事件
- // @param call->return HR_ 开头常量
- func (t *HttpServer) OnPrepareListen(call func(t *HttpServer, Sender, Listen uintptr) HR) *HttpServer {
- if !t.isValid() {
- return t
- }
- if call == nil {
- HP_Set_FN_HttpServer_OnPrepareListen.Call(t.hListener, 0)
- } else {
- t.onPrepareListen = call
- HP_Set_FN_HttpServer_OnPrepareListen.Call(t.hListener, server_callback_OnPrepareListen_ptr)
- }
- return t
- }
- // OnAccept
- //
- // @Description: 【可选】绑定接受事件
- // @param call->return HR_ 开头常量
- func (t *HttpServer) OnAccept(call func(t *HttpServer, Sender, ConnID, Client uintptr) HR) *HttpServer {
- if !t.isValid() {
- return t
- }
- if call == nil {
- HP_Set_FN_HttpServer_OnAccept.Call(t.hListener, 0)
- } else {
- t.onAccept = call
- HP_Set_FN_HttpServer_OnAccept.Call(t.hListener, server_callback_OnAccept_ptr)
- }
- return t
- }
- // OnHandShake
- //
- // @Description: 【可选】绑定握手事件
- // @param call->return HR_ 开头常量
- func (t *HttpServer) OnHandShake(call func(t *HttpServer, Sender, ConnID uintptr) HR) *HttpServer {
- if !t.isValid() {
- return t
- }
- if call == nil {
- HP_Set_FN_HttpServer_OnHandShake.Call(t.hListener, 0)
- } else {
- t.onHandShake = call
- HP_Set_FN_HttpServer_OnHandShake.Call(t.hListener, server_callback_OnHandShake_ptr)
- }
- return t
- }
- // OnSend
- //
- // @Description: 【可选】绑定发送事件
- // @param call->return HR_ 开头常量
- func (t *HttpServer) OnSend(call func(t *HttpServer, Sender, ConnID, Data uintptr, Length uintptr) HR) *HttpServer {
- if !t.isValid() {
- return t
- }
- if call == nil {
- HP_Set_FN_HttpServer_OnSend.Call(t.hListener, 0)
- } else {
- t.onSend = call
- HP_Set_FN_HttpServer_OnSend.Call(t.hListener, server_callback_OnSend_ptr)
- }
- return t
- }
- // OnReceive
- //
- // @Description: 【必选】绑定数据接收事件
- // @param call->return HR_ 开头常量
- func (t *HttpServer) OnReceive(call func(t *HttpServer, Sender, ConnID, Data uintptr, Length uintptr) HR) *HttpServer {
- if !t.isValid() {
- return t
- }
- if call == nil {
- HP_Set_FN_HttpServer_OnReceive.Call(t.hListener, 0)
- } else {
- t.onReceive = call
- HP_Set_FN_HttpServer_OnReceive.Call(t.hListener, server_callback_OnReceive_ptr)
- }
- return t
- }
- // OnClose
- //
- // @Description: 【必选】绑定断开事件
- // @param call->Operation 通过该参数标识是哪种操作导致的错误 SO_ 开头常量
- // @param call->return HR_ 开头常量
- func (t *HttpServer) OnClose(call func(t *HttpServer, Sender, ConnID uintptr, Operation SO, ErrorCode int) HR) *HttpServer {
- if !t.isValid() {
- return t
- }
- if call == nil {
- HP_Set_FN_HttpServer_OnClose.Call(t.hListener, 0)
- } else {
- t.onClose = call
- HP_Set_FN_HttpServer_OnClose.Call(t.hListener, server_callback_OnClose_ptr)
- }
- return t
- }
- // OnShutdown
- //
- // @Description: 【可选】绑定关闭事件
- // @param call->return HR_ 开头常量
- func (t *HttpServer) OnShutdown(call func(t *HttpServer, Sender uintptr) HR) *HttpServer {
- if !t.isValid() {
- return t
- }
- if call == nil {
- HP_Set_FN_HttpServer_OnShutdown.Call(t.hListener, 0)
- } else {
- t.onShutdown = call
- HP_Set_FN_HttpServer_OnShutdown.Call(t.hListener, server_callback_OnShutdown_ptr)
- }
- return t
- }
- // OnDefault
- //
- // @Description: 【可选】绑定所有【必选】事件
- func (t *HttpServer) OnDefault() *HttpServer {
- if !t.isValid() {
- return t
- }
- if t.onHeadersComplete == nil {
- t.OnHeadersComplete(func(t *HttpServer, Sender, ConnID uintptr) HPR {
- return HPR_OK
- })
- }
- if t.onBody == nil {
- t.OnBody(func(t *HttpServer, Sender, ConnID, Data uintptr, Length uintptr) HPR {
- return HPR_OK
- })
- }
- if t.onMessageComplete == nil {
- t.OnMessageComplete(func(t *HttpServer, Sender, ConnID uintptr) HPR {
- return HPR_OK
- })
- }
- if t.onParseError == nil {
- t.OnParseError(func(t *HttpServer, Sender, ConnID uintptr, ErrorCode int, ErrorDesc string) HPR {
- return HPR_OK
- })
- }
- if t.onReceive == nil {
- t.OnReceive(func(t *HttpServer, Sender, ConnID, Data uintptr, Length uintptr) HR {
- return HR_OK
- })
- }
- if t.onClose == nil {
- t.OnClose(func(t *HttpServer, Sender, ConnID uintptr, Operation SO, ErrorCode int) HR {
- return HR_OK
- })
- }
- return t
- }
- // --------------------------------------------------------------------------------------------------------------[TNVPairs]
- // TNVPair 描述:字符串名值对结构体
- type TNVPair struct {
- Name string
- Value string
- }
|