server_http_windows.go 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034
  1. //go:build windows
  2. package yu_hpsocket
  3. import (
  4. yu_http "gogs.qqck.cn/s/tools/http"
  5. yu_strings "gogs.qqck.cn/s/tools/strings"
  6. yu_sys "gogs.qqck.cn/s/tools/sys"
  7. "unsafe"
  8. )
  9. // HttpServer
  10. // @Description: HttpServerListener 对象,请自己创建和销毁,目的是保证安全
  11. type HttpServer struct {
  12. Server[HttpServer]
  13. TcpServer
  14. // 事件----------------------------------
  15. onMessageBegin func(t *HttpServer, Sender, ConnID uintptr) HPR
  16. onRequestLine func(t *HttpServer, Sender, ConnID uintptr, Method, Url string) HPR
  17. onHeader func(t *HttpServer, Sender, ConnID uintptr, Name, Value string) HPR
  18. onHeadersComplete func(t *HttpServer, Sender, ConnID uintptr) HPR
  19. onBody func(t *HttpServer, Sender, ConnID, Data uintptr, Length uintptr) HPR
  20. onChunkHeader func(t *HttpServer, Sender, ConnID uintptr, Length uintptr) HPR
  21. onChunkComplete func(t *HttpServer, Sender, ConnID uintptr) HPR
  22. onMessageComplete func(t *HttpServer, Sender, ConnID uintptr) HPR
  23. onUpgrade func(t *HttpServer, Sender, ConnID uintptr, UpgradeType HUT) HPR
  24. onParseError func(t *HttpServer, Sender, ConnID uintptr, ErrorCode int, ErrorDesc string) HPR
  25. onWSMessageHeader func(t *HttpServer, Sender, ConnID uintptr, Final bool, Reserved, OperationCode byte, Mask []byte, BodyLen int64) HR
  26. onWSMessageBody func(t *HttpServer, Sender, ConnID, Data uintptr, Length uintptr) HR
  27. onWSMessageComplete func(t *HttpServer, Sender, ConnID uintptr) HR
  28. }
  29. // NewHttpServer
  30. //
  31. // @Description: 创建 HttpServer 对象
  32. func NewHttpServer() (t *HttpServer) {
  33. t = &HttpServer{}
  34. return
  35. }
  36. // Create
  37. //
  38. // @Description: 创建 HttpServer 环境
  39. func (t *HttpServer) Create(SSL bool) bool {
  40. t.Close()
  41. if t.hListener, _, _ = Create_HP_HttpServerListener.Call(); t.hListener == 0 {
  42. return false
  43. }
  44. if t.SSL = SSL; t.SSL {
  45. t.hServer, _, _ = Create_HP_HttpsServer.Call(t.hListener)
  46. } else {
  47. t.hServer, _, _ = Create_HP_HttpServer.Call(t.hListener)
  48. }
  49. if t.hServer == 0 {
  50. Destroy_HP_HttpServerListener.Call(t.hListener)
  51. t.hListener = 0
  52. return false
  53. }
  54. _server_on_set(t.hServer, &_server_item{
  55. _type: _server_type_HttpServer,
  56. t: t,
  57. })
  58. HP_Set_FN_HttpServer_OnHeader.Call(t.hListener, server_http_callback_OnHeader_ptr) // 默认绑定,OnHeader 有问题,会内存溢出
  59. return true
  60. }
  61. // Close
  62. //
  63. // @Description: 销毁 HttpServer 环境
  64. func (t *HttpServer) Close() {
  65. t.Stop()
  66. t.Wait(-1)
  67. if t.hServer != 0 {
  68. if t.SSL {
  69. Destroy_HP_HttpsServer.Call(t.hServer)
  70. } else {
  71. Destroy_HP_HttpServer.Call(t.hServer)
  72. }
  73. _server_on_del(t.hServer)
  74. t.hServer = 0
  75. }
  76. if t.hListener != 0 {
  77. Destroy_HP_HttpServerListener.Call(t.hListener)
  78. t.hListener = 0
  79. }
  80. }
  81. // SendResponse
  82. //
  83. // @Description: 向客户端回复 HTTP 请求
  84. // @param ConnID 连接 ID
  85. // @param StatusCode HTTP 状态码
  86. // @param Desc HTTP 状态描述,空为默认
  87. // @param Headers 回复请求头
  88. // @param Data 回复请求体
  89. func (t *HttpServer) SendResponse(ConnID uintptr, StatusCode yu_http.Status, Desc string, Headers []TNVPair, Data []byte) bool {
  90. if !t.isValid() {
  91. return false
  92. }
  93. if Desc == "" {
  94. Desc = yu_http.StatusText(StatusCode)
  95. }
  96. j_headers_buf := make([][2][]byte, len(Headers))
  97. j_headers := make([][2]unsafe.Pointer, len(Headers))
  98. var j_headers_ptr unsafe.Pointer
  99. if len(Headers) > 0 {
  100. for j_i := 0; j_i < len(Headers); j_i++ {
  101. j_headers_buf[j_i][0] = yu_strings.Utf8ToGbkBytes(Headers[j_i].Name)
  102. j_headers[j_i][0] = unsafe.Pointer(&j_headers_buf[j_i][0][0])
  103. j_headers_buf[j_i][1] = yu_strings.Utf8ToGbkBytes(Headers[j_i].Value)
  104. j_headers[j_i][1] = unsafe.Pointer(&j_headers_buf[j_i][1][0])
  105. }
  106. j_headers_ptr = unsafe.Pointer(&j_headers[0])
  107. }
  108. return HP_HttpServer_SendResponse.CallBool(t.hServer, ConnID,
  109. uintptr(StatusCode), yu_sys.S{yu_sys.Gbk, Desc},
  110. j_headers_ptr, len(Headers), Data, len(Data),
  111. )
  112. }
  113. // SendResponseS
  114. //
  115. // @Description: 向客户端回复 HTTP 请求
  116. // @param ConnID 连接 ID
  117. // @param StatusCode HTTP 状态码
  118. // @param Desc HTTP 状态描述,空为默认
  119. // @param Headers 回复请求头
  120. // @param Data 回复请求体
  121. func (t *HttpServer) SendResponseS(ConnID uintptr, StatusCode yu_http.Status, Desc string, Headers []TNVPair, Data string) bool {
  122. if !t.isValid() {
  123. return false
  124. }
  125. if Desc == "" {
  126. Desc = yu_http.StatusText(StatusCode)
  127. }
  128. j_headers_buf := make([][2][]byte, len(Headers))
  129. j_headers := make([][2]unsafe.Pointer, len(Headers))
  130. var j_headers_ptr unsafe.Pointer
  131. if len(Headers) > 0 {
  132. for j_i := 0; j_i < len(Headers); j_i++ {
  133. j_headers_buf[j_i][0] = yu_strings.Utf8ToGbkBytes(Headers[j_i].Name)
  134. j_headers[j_i][0] = unsafe.Pointer(&j_headers_buf[j_i][0][0])
  135. j_headers_buf[j_i][1] = yu_strings.Utf8ToGbkBytes(Headers[j_i].Value)
  136. j_headers[j_i][1] = unsafe.Pointer(&j_headers_buf[j_i][1][0])
  137. }
  138. j_headers_ptr = unsafe.Pointer(&j_headers[0])
  139. }
  140. return HP_HttpServer_SendResponse.CallBool(t.hServer, ConnID,
  141. uintptr(StatusCode), yu_sys.S{yu_sys.Gbk, Desc},
  142. j_headers_ptr, len(Headers), Data, len(Data),
  143. )
  144. }
  145. // SendChunkData
  146. //
  147. // @Description: 向对端发送 Chunked 数据分片
  148. // @param ConnID 连接 ID
  149. // @param Data Chunked 数据分片(为 nil 或 len(data)=0 表示结束分片)
  150. // @param Extensions 扩展属性
  151. func (t *HttpServer) SendChunkData(ConnID uintptr, Data []byte, Extensions int) bool {
  152. if !t.isValid() {
  153. return false
  154. }
  155. return HP_HttpServer_SendChunkData.CallBool(t.hServer, ConnID, Data, len(Data), Extensions)
  156. }
  157. // SendChunkDataS
  158. //
  159. // @Description: 向对端发送 Chunked 数据分片
  160. // @param ConnID 连接 ID
  161. // @param Data Chunked 数据分片(为 nil 或 len(data)=0 表示结束分片)
  162. // @param Extensions 扩展属性
  163. func (t *HttpServer) SendChunkDataS(ConnID uintptr, Data string, Extensions int) bool {
  164. if !t.isValid() {
  165. return false
  166. }
  167. return HP_HttpServer_SendChunkData.CallBool(t.hServer, ConnID, Data, len(Data), Extensions)
  168. }
  169. // SendLocalFile
  170. //
  171. // @Description: 向指定连接发送 4096 KB 以下的小文件
  172. // @param ConnID 连接 ID
  173. // @param FileName 文件路径
  174. // @param StatusCode HTTP 状态码
  175. // @param Desc HTTP 状态描述,空为默认
  176. // @param Headers 回复请求头
  177. func (t *HttpServer) SendLocalFile(ConnID uintptr, FileName string, StatusCode yu_http.Status, Desc string, Headers []TNVPair) bool {
  178. if !t.isValid() {
  179. return false
  180. }
  181. if Desc == "" {
  182. Desc = yu_http.StatusText(StatusCode)
  183. }
  184. j_headers_buf := make([][2][]byte, len(Headers))
  185. j_headers := make([][2]unsafe.Pointer, len(Headers))
  186. var j_headers_ptr unsafe.Pointer
  187. if len(Headers) > 0 {
  188. for j_i := 0; j_i < len(Headers); j_i++ {
  189. j_headers_buf[j_i][0] = yu_strings.Utf8ToGbkBytes(Headers[j_i].Name)
  190. j_headers[j_i][0] = unsafe.Pointer(&j_headers_buf[j_i][0][0])
  191. j_headers_buf[j_i][1] = yu_strings.Utf8ToGbkBytes(Headers[j_i].Value)
  192. j_headers[j_i][1] = unsafe.Pointer(&j_headers_buf[j_i][1][0])
  193. }
  194. j_headers_ptr = unsafe.Pointer(&j_headers[0])
  195. }
  196. return HP_HttpServer_SendLocalFile.CallBool(t.hServer, ConnID,
  197. yu_sys.S{yu_sys.Gbk, FileName},
  198. uintptr(StatusCode), yu_sys.S{yu_sys.Gbk, Desc},
  199. j_headers_ptr, len(Headers),
  200. )
  201. }
  202. // SendWSMessage
  203. //
  204. // @Description: 向对端端发送 WebSocket 消息
  205. // @param ConnID 连接 ID
  206. // @param Final 是否结束帧
  207. // @param Reserved RSV1/RSV2/RSV3 各 1 位
  208. // @param OperationCode 操作码:0x0 - 0xF
  209. // @param Data 消息体数据缓冲区
  210. // @param BodyLen 消息总长度
  211. func (t *HttpServer) SendWSMessage(ConnID uintptr, Final bool, Reserved, OperationCode byte, Data []byte, BodyLen int64) bool {
  212. if !t.isValid() {
  213. return false
  214. }
  215. return HP_HttpServer_SendWSMessage.CallBool(t.hServer, ConnID, Final, Reserved, OperationCode, Data, len(Data), BodyLen)
  216. }
  217. // SendWSMessageS
  218. //
  219. // @Description: 向对端端发送 WebSocket 消息
  220. // @param ConnID 连接 ID
  221. // @param Final 是否结束帧
  222. // @param Reserved RSV1/RSV2/RSV3 各 1 位
  223. // @param OperationCode 操作码:0x0 - 0xF
  224. // @param Data 消息体数据缓冲区
  225. // @param BodyLen 消息总长度
  226. func (t *HttpServer) SendWSMessageS(ConnID uintptr, Final bool, Reserved, OperationCode byte, Data string, BodyLen int64) bool {
  227. if !t.isValid() {
  228. return false
  229. }
  230. return HP_HttpServer_SendWSMessage.CallBool(t.hServer, ConnID, Final, Reserved, OperationCode, Data, len(Data), BodyLen)
  231. }
  232. // Release
  233. //
  234. // @Description: 把连接放入释放队列,等待某个时间(通过 SetReleaseDelay() 设置)关闭连接
  235. // @param ConnID 连接 ID
  236. func (t *HttpServer) Release(ConnID uintptr) bool {
  237. if !t.isValid() {
  238. return false
  239. }
  240. return HP_HttpServer_Release.CallBool(t.hServer, ConnID)
  241. }
  242. // SetReleaseDelay
  243. //
  244. // @Description: 设置连接释放延时(默认:3000 毫秒)
  245. func (t *HttpServer) SetReleaseDelay(ReleaseDelay int) *HttpServer {
  246. if !t.isValid() {
  247. return t
  248. }
  249. HP_HttpServer_SetReleaseDelay.Call(t.hServer, ReleaseDelay)
  250. return t
  251. }
  252. // GetReleaseDelay
  253. //
  254. // @Description: 获取连接释放延时
  255. func (t *HttpServer) GetReleaseDelay() int {
  256. if !t.isValid() {
  257. return 0
  258. }
  259. return HP_HttpServer_GetReleaseDelay.CallInt(t.hServer)
  260. }
  261. // GetUrlFieldSet
  262. //
  263. // @Description: 获取请求行 URL 域掩码(URL 域参考:EnHttpUrlField)
  264. // @param ConnID 连接 ID
  265. func (t *HttpServer) GetUrlFieldSet(ConnID uintptr) HUF {
  266. if !t.isValid() {
  267. return 0
  268. }
  269. return HUF(HP_HttpServer_GetUrlFieldSet.CallUintptr(t.hServer, ConnID))
  270. }
  271. // GetUrlField
  272. //
  273. // @Description: 获取某个 URL 域值
  274. // @param ConnID 连接 ID
  275. // @param Field HUF_ 开头常量
  276. func (t *HttpServer) GetUrlField(ConnID uintptr, Field HUF) string {
  277. if !t.isValid() {
  278. return ""
  279. }
  280. j_value := HP_HttpServer_GetUrlField.CallGbkToUtf8(t.hServer, ConnID, uintptr(Field))
  281. if Field == HUF_PATH {
  282. if j_value == "" {
  283. j_value = "/"
  284. } else if j_value[len(j_value)-1] != '/' {
  285. j_value += "/"
  286. } else {
  287. for j_i := len(j_value) - 1; j_i > -1; j_i-- {
  288. if j_value[j_i] == '/' {
  289. j_value = j_value[:j_i+1]
  290. } else {
  291. break
  292. }
  293. }
  294. }
  295. }
  296. return j_value
  297. }
  298. // GetMethod
  299. //
  300. // @Description: 获取请求方法
  301. // @param ConnID v
  302. func (t *HttpServer) GetMethod(ConnID uintptr) string {
  303. if !t.isValid() {
  304. return ""
  305. }
  306. return HP_HttpServer_GetMethod.CallGbkToUtf8(t.hServer, ConnID)
  307. }
  308. // SetLocalVersion
  309. //
  310. // @Description: 设置本地协议版本
  311. func (t *HttpServer) SetLocalVersion(Version HV) *HttpServer {
  312. if !t.isValid() {
  313. return t
  314. }
  315. HP_HttpServer_SetLocalVersion.Call(t.hServer, uintptr(Version))
  316. return t
  317. }
  318. // GetLocalVersion
  319. //
  320. // @Description: 获取本地协议版本
  321. func (t *HttpServer) GetLocalVersion() HV {
  322. if !t.isValid() {
  323. return 0
  324. }
  325. return HV(HP_HttpServer_GetLocalVersion.CallUintptr(t.hServer))
  326. }
  327. // IsUpgrade
  328. //
  329. // @Description: 检查是否升级协议
  330. // @param ConnID 连接 ID
  331. func (t *HttpServer) IsUpgrade(ConnID uintptr) bool {
  332. if !t.isValid() {
  333. return false
  334. }
  335. return HP_HttpServer_IsUpgrade.CallBool(t.hServer, ConnID)
  336. }
  337. // IsKeepAlive
  338. //
  339. // @Description: 检查是否有 Keep-Alive 标识
  340. // @param ConnID 连接 ID
  341. func (t *HttpServer) IsKeepAlive(ConnID uintptr) bool {
  342. if !t.isValid() {
  343. return false
  344. }
  345. return HP_HttpServer_IsKeepAlive.CallBool(t.hServer, ConnID)
  346. }
  347. // StartHttp
  348. //
  349. // @Description: 启动 HTTP 通信,当通信组件设置为非自动启动 HTTP 通信时,需要调用本方法启动 HTTP 通信
  350. // @param ConnID 连接 ID
  351. func (t *HttpServer) StartHttp(ConnID uintptr) bool {
  352. if !t.isValid() {
  353. return false
  354. }
  355. return HP_HttpServer_StartHttp.CallBool(t.hServer, ConnID)
  356. }
  357. // SetHttpAutoStart
  358. //
  359. // @Description: 设置 HTTP 启动方式(默认:TRUE,自动启动)
  360. func (t *HttpServer) SetHttpAutoStart(AutoStart bool) *HttpServer {
  361. if !t.isValid() {
  362. return t
  363. }
  364. HP_HttpServer_SetHttpAutoStart.Call(t.hServer, AutoStart)
  365. return t
  366. }
  367. // IsHttpAutoStart
  368. //
  369. // @Description: 获取 HTTP 启动方式
  370. func (t *HttpServer) IsHttpAutoStart() bool {
  371. if !t.isValid() {
  372. return false
  373. }
  374. return HP_HttpServer_IsHttpAutoStart.CallBool(t.hServer)
  375. }
  376. // GetVersion
  377. //
  378. // @Description: 获取协议版本
  379. // @param ConnID ConnID 连接 ID
  380. func (t *HttpServer) GetVersion(ConnID uintptr) HV {
  381. if !t.isValid() {
  382. return 0
  383. }
  384. return HV(HP_HttpServer_GetVersion.CallUintptr(t.hServer, ConnID))
  385. }
  386. // GetHost
  387. //
  388. // @Description: 获取主机
  389. // @param ConnID 连接 ID
  390. func (t *HttpServer) GetHost(ConnID uintptr) string {
  391. if !t.isValid() {
  392. return ""
  393. }
  394. return HP_HttpServer_GetHost.CallGbkToUtf8(t.hServer, ConnID)
  395. }
  396. // GetContentLength
  397. //
  398. // @Description: 获取内容长度
  399. // @param ConnID 连接 ID
  400. func (t *HttpServer) GetContentLength(ConnID uintptr) int64 {
  401. if !t.isValid() {
  402. return 0
  403. }
  404. return HP_HttpServer_GetContentLength.CallInt64(t.hServer, ConnID)
  405. }
  406. // GetContentType
  407. //
  408. // @Description: 获取内容类型
  409. // @param ConnID 连接 ID
  410. func (t *HttpServer) GetContentType(ConnID uintptr) string {
  411. if !t.isValid() {
  412. return ""
  413. }
  414. return HP_HttpServer_GetContentType.CallGbkToUtf8(t.hServer, ConnID)
  415. }
  416. // GetContentEncoding
  417. //
  418. // @Description: 获取内容编码
  419. // @param ConnID 连接 ID
  420. func (t *HttpServer) GetContentEncoding(ConnID uintptr) string {
  421. if !t.isValid() {
  422. return ""
  423. }
  424. return HP_HttpServer_GetContentEncoding.CallGbkToUtf8(t.hServer, ConnID)
  425. }
  426. // GetTransferEncoding
  427. //
  428. // @Description: 获取传输编码
  429. // @param ConnID 连接 ID
  430. func (t *HttpServer) GetTransferEncoding(ConnID uintptr) string {
  431. if !t.isValid() {
  432. return ""
  433. }
  434. return HP_HttpServer_GetTransferEncoding.CallGbkToUtf8(t.hServer, ConnID)
  435. }
  436. // GetUpgradeType
  437. //
  438. // @Description: 获取协议升级类型
  439. // @param ConnID 连接 ID
  440. func (t *HttpServer) GetUpgradeType(ConnID uintptr) HUT {
  441. if !t.isValid() {
  442. return 0
  443. }
  444. return HUT(HP_HttpServer_GetUpgradeType.CallInt(t.hServer, ConnID))
  445. }
  446. // GetParseErrorCode
  447. //
  448. // @Description: 获取解析错误代码
  449. // @param ConnID 连接 ID
  450. // @return ErrorCode
  451. // @return ErrorDesc
  452. func (t *HttpServer) GetParseErrorCode(ConnID uintptr) (ErrorCode int, ErrorDesc string) {
  453. if !t.isValid() {
  454. return
  455. }
  456. var j_ErrorDesc uintptr
  457. ErrorCode = HP_HttpServer_GetParseErrorCode.CallInt(t.hServer, ConnID, &j_ErrorDesc)
  458. ErrorDesc = yu_strings.GbkPtrToUtf8(j_ErrorDesc)
  459. return
  460. }
  461. // GetHeader
  462. //
  463. // @Description: 获取某个请求头(单值)
  464. // @param ConnID 连接 ID
  465. func (t *HttpServer) GetHeader(ConnID uintptr, Name string) string {
  466. if !t.isValid() {
  467. return ""
  468. }
  469. var j_value uintptr
  470. if !HP_HttpServer_GetHeader.CallBool(t.hServer, ConnID,
  471. yu_sys.S{yu_sys.Gbk, Name}, &j_value,
  472. ) {
  473. return ""
  474. }
  475. return yu_strings.GbkPtrToUtf8(j_value)
  476. }
  477. // GetHeaders
  478. //
  479. // @Description: 获取某个请求头(多值)
  480. // @param ConnID 连接 ID
  481. func (t *HttpServer) GetHeaders(ConnID uintptr, Name string) (values []string) {
  482. if !t.isValid() {
  483. return
  484. }
  485. var j_count int
  486. HP_HttpServer_GetHeaders.Call(t.hServer, ConnID, yu_sys.S{yu_sys.Gbk, Name}, 0, &j_count)
  487. if j_count == 0 {
  488. return
  489. }
  490. j_values := make([]uintptr, j_count)
  491. if !HP_HttpServer_GetHeaders.CallBool(t.hServer, ConnID, yu_sys.S{yu_sys.Gbk, Name}, j_values, &j_count) {
  492. return
  493. }
  494. values = make([]string, 0, j_count)
  495. for _, j_value := range j_values {
  496. values = append(values, yu_strings.GbkPtrToUtf8(j_value))
  497. }
  498. return
  499. }
  500. // GetAllHeaders
  501. //
  502. // @Description: 取得所有HTTP返回协议头
  503. // @param ConnID 连接 ID
  504. func (t *HttpServer) GetAllHeaders(ConnID uintptr) (values []TNVPair) {
  505. if !t.isValid() {
  506. return
  507. }
  508. var j_count int
  509. HP_HttpServer_GetAllHeaders.Call(t.hServer, ConnID, 0, &j_count)
  510. if j_count == 0 {
  511. return
  512. }
  513. j_values := make([][2]uintptr, j_count)
  514. if !HP_HttpServer_GetAllHeaders.CallBool(t.hServer, ConnID, unsafe.Pointer(&j_values[0]), &j_count) {
  515. return
  516. }
  517. values = make([]TNVPair, j_count)
  518. for j_i := 0; j_i < j_count; j_i++ {
  519. values[j_i].Name = yu_strings.GbkPtrToUtf8(j_values[j_i][0])
  520. values[j_i].Value = yu_strings.GbkPtrToUtf8(j_values[j_i][1])
  521. }
  522. return
  523. }
  524. // GetAllHeaderNames
  525. //
  526. // @Description: 获取所有请求头名称
  527. // @param ConnID 连接 ID
  528. func (t *HttpServer) GetAllHeaderNames(ConnID uintptr) (values []string) {
  529. if !t.isValid() {
  530. return
  531. }
  532. var j_count int
  533. HP_HttpServer_GetAllHeaderNames.Call(t.hServer, ConnID, 0, &j_count)
  534. if j_count == 0 {
  535. return
  536. }
  537. j_values := make([]uintptr, j_count)
  538. if !HP_HttpServer_GetAllHeaderNames.CallBool(t.hServer, ConnID, j_values, &j_count) {
  539. return
  540. }
  541. values = make([]string, 0, j_count)
  542. for _, j_value := range j_values {
  543. values = append(values, yu_strings.GbkPtrToUtf8(j_value))
  544. }
  545. return
  546. }
  547. // GetCookie
  548. //
  549. // @Description: 获取 Cookie
  550. // @param ConnID 连接 ID
  551. func (t *HttpServer) GetCookie(ConnID uintptr, Name string) string {
  552. if !t.isValid() {
  553. return ""
  554. }
  555. var j_value uintptr
  556. if !HP_HttpServer_GetCookie.CallBool(t.hServer, ConnID,
  557. yu_sys.S{yu_sys.Gbk, Name}, &j_value,
  558. ) {
  559. return ""
  560. }
  561. return yu_strings.GbkPtrToUtf8(j_value)
  562. }
  563. // GetAllCookies
  564. //
  565. // @Description: 获取所有 Cookie
  566. // @param ConnID 连接 ID
  567. func (t *HttpServer) GetAllCookies(ConnID uintptr) (values []TNVPair) {
  568. if !t.isValid() {
  569. return
  570. }
  571. var j_count int
  572. HP_HttpServer_GetAllCookies.Call(t.hServer, ConnID, 0, &j_count)
  573. if j_count == 0 {
  574. return
  575. }
  576. j_values := make([][2]uintptr, j_count)
  577. if !HP_HttpServer_GetAllCookies.CallBool(t.hServer, ConnID, unsafe.Pointer(&j_values[0]), &j_count) {
  578. return
  579. }
  580. values = make([]TNVPair, j_count)
  581. for j_i := 0; j_i < j_count; j_i++ {
  582. values[j_i].Name = yu_strings.GbkPtrToUtf8(j_values[j_i][0])
  583. values[j_i].Value = yu_strings.GbkPtrToUtf8(j_values[j_i][1])
  584. }
  585. return
  586. }
  587. // GetWSMessageState
  588. //
  589. // @Description: 获取当前 WebSocket 消息状态
  590. // @param ConnID 连接 ID
  591. func (t *HttpServer) GetWSMessageState(ConnID uintptr) (Final bool, Reserved, OperationCode byte, Mask [4]byte, BodyLen int64, BodyRemain int64) {
  592. var j_Mask uintptr
  593. if !HP_HttpServer_GetWSMessageState.CallBool(t.hServer, ConnID, &Final, &Reserved, &OperationCode, &j_Mask, &BodyLen, &BodyRemain) {
  594. return
  595. }
  596. *((*uintptr)(unsafe.Pointer(&Mask))) = j_Mask
  597. return
  598. }
  599. // OnMessageBegin
  600. //
  601. // @Description: 【可选】绑定开始解析事件
  602. // @param call->return HPR_ 开头常量
  603. func (t *HttpServer) OnMessageBegin(call func(t *HttpServer, Sender, ConnID uintptr) HPR) *HttpServer {
  604. if !t.isValid() {
  605. return t
  606. }
  607. if call == nil {
  608. HP_Set_FN_HttpServer_OnMessageBegin.Call(t.hListener, 0)
  609. } else {
  610. t.onMessageBegin = call
  611. HP_Set_FN_HttpServer_OnMessageBegin.Call(t.hListener, server_http_callback_OnMessageBegin_ptr)
  612. }
  613. return t
  614. }
  615. // OnRequestLine
  616. //
  617. // @Description: 【可选】绑定请求行解析完成事件
  618. // @param call->return HPR_ 开头常量
  619. func (t *HttpServer) OnRequestLine(call func(t *HttpServer, Sender, ConnID uintptr, Method, Url string) HPR) *HttpServer {
  620. if !t.isValid() {
  621. return t
  622. }
  623. if call == nil {
  624. HP_Set_FN_HttpServer_OnRequestLine.Call(t.hListener, 0)
  625. } else {
  626. t.onRequestLine = call
  627. HP_Set_FN_HttpServer_OnRequestLine.Call(t.hListener, server_http_callback_OnRequestLine_ptr)
  628. }
  629. return t
  630. }
  631. // _OnHeader
  632. //
  633. // @Description: 【可选】绑定请求头事件
  634. // @param call->return HPR_ 开头常量
  635. func (t *HttpServer) _OnHeader(call func(t *HttpServer, Sender, ConnID uintptr, Name, Value string) HPR) *HttpServer {
  636. if !t.isValid() {
  637. return t
  638. }
  639. if call == nil {
  640. HP_Set_FN_HttpServer_OnHeader.Call(t.hListener, 0)
  641. } else {
  642. t.onHeader = call
  643. HP_Set_FN_HttpServer_OnHeader.Call(t.hListener, server_http_callback_OnHeader_ptr)
  644. }
  645. return t
  646. }
  647. // OnHeadersComplete
  648. //
  649. // @Description: 【必选】绑定请求头完成事件
  650. // @param call->return HPR_ 开头常量
  651. func (t *HttpServer) OnHeadersComplete(call func(t *HttpServer, Sender, ConnID uintptr) HPR) *HttpServer {
  652. if !t.isValid() {
  653. return t
  654. }
  655. if call == nil {
  656. HP_Set_FN_HttpServer_OnHeadersComplete.Call(t.hListener, 0)
  657. } else {
  658. t.onHeadersComplete = call
  659. HP_Set_FN_HttpServer_OnHeadersComplete.Call(t.hListener, server_http_callback_OnHeadersComplete_ptr)
  660. }
  661. return t
  662. }
  663. // OnBody
  664. //
  665. // @Description: 【必选】绑定请求体报文事件
  666. // @param call->return HPR_ 开头常量
  667. func (t *HttpServer) OnBody(call func(t *HttpServer, Sender, ConnID, Data uintptr, Length uintptr) HPR) *HttpServer {
  668. if !t.isValid() {
  669. return t
  670. }
  671. if call == nil {
  672. HP_Set_FN_HttpServer_OnBody.Call(t.hListener, 0)
  673. } else {
  674. t.onBody = call
  675. HP_Set_FN_HttpServer_OnBody.Call(t.hListener, server_http_callback_OnBody_ptr)
  676. }
  677. return t
  678. }
  679. // OnChunkHeader
  680. //
  681. // @Description: 【可选】绑定Chunked 报文头事件
  682. // @param call->return HPR_ 开头常量
  683. func (t *HttpServer) OnChunkHeader(call func(t *HttpServer, Sender, ConnID uintptr, Length uintptr) HPR) *HttpServer {
  684. if !t.isValid() {
  685. return t
  686. }
  687. if call == nil {
  688. HP_Set_FN_HttpServer_OnChunkHeader.Call(t.hListener, 0)
  689. } else {
  690. t.onChunkHeader = call
  691. HP_Set_FN_HttpServer_OnChunkHeader.Call(t.hListener, server_http_callback_OnChunkHeader_ptr)
  692. }
  693. return t
  694. }
  695. // OnChunkComplete
  696. //
  697. // @Description: 【可选】绑定Chunked 报文完成事件
  698. // @param call->return HPR_ 开头常量
  699. func (t *HttpServer) OnChunkComplete(call func(t *HttpServer, Sender, ConnID uintptr) HPR) *HttpServer {
  700. if !t.isValid() {
  701. return t
  702. }
  703. if call == nil {
  704. HP_Set_FN_HttpServer_OnChunkComplete.Call(t.hListener, 0)
  705. } else {
  706. t.onChunkComplete = call
  707. HP_Set_FN_HttpServer_OnChunkComplete.Call(t.hListener, server_http_callback_OnChunkComplete_ptr)
  708. }
  709. return t
  710. }
  711. // OnMessageComplete
  712. //
  713. // @Description: 【必选】绑定完成解析事件
  714. // @param call->return HPR_ 开头常量
  715. func (t *HttpServer) OnMessageComplete(call func(t *HttpServer, Sender, ConnID uintptr) HPR) *HttpServer {
  716. if !t.isValid() {
  717. return t
  718. }
  719. if call == nil {
  720. HP_Set_FN_HttpServer_OnMessageComplete.Call(t.hListener, 0)
  721. } else {
  722. t.onMessageComplete = call
  723. HP_Set_FN_HttpServer_OnMessageComplete.Call(t.hListener, server_http_callback_OnMessageComplete_ptr)
  724. }
  725. return t
  726. }
  727. // OnUpgrade
  728. //
  729. // @Description: 【可选】绑定升级协议事件
  730. // @param call->return HPR_ 开头常量
  731. func (t *HttpServer) OnUpgrade(call func(t *HttpServer, Sender, ConnID uintptr, UpgradeType HUT) HPR) *HttpServer {
  732. if !t.isValid() {
  733. return t
  734. }
  735. if call == nil {
  736. HP_Set_FN_HttpServer_OnUpgrade.Call(t.hListener, 0)
  737. } else {
  738. t.onUpgrade = call
  739. HP_Set_FN_HttpServer_OnUpgrade.Call(t.hListener, server_http_callback_OnUpgrade_ptr)
  740. }
  741. return t
  742. }
  743. // OnParseError
  744. //
  745. // @Description: 【必选】绑定解析错误事件
  746. // @param call->return HPR_ 开头常量
  747. func (t *HttpServer) OnParseError(call func(t *HttpServer, Sender, ConnID uintptr, ErrorCode int, ErrorDesc string) HPR) *HttpServer {
  748. if !t.isValid() {
  749. return t
  750. }
  751. if call == nil {
  752. HP_Set_FN_HttpServer_OnParseError.Call(t.hListener, 0)
  753. } else {
  754. t.onParseError = call
  755. HP_Set_FN_HttpServer_OnParseError.Call(t.hListener, server_http_callback_OnParseError_ptr)
  756. }
  757. return t
  758. }
  759. // OnWSMessageHeader
  760. //
  761. // @Description: 【可选】绑定WebSocket 数据包头事件
  762. // @param call->return HR_ 开头常量
  763. func (t *HttpServer) OnWSMessageHeader(call func(t *HttpServer, Sender, ConnID uintptr, Final bool, Reserved, OperationCode byte, Mask []byte, BodyLen int64) HR) *HttpServer {
  764. if !t.isValid() {
  765. return t
  766. }
  767. if call == nil {
  768. HP_Set_FN_HttpServer_OnWSMessageHeader.Call(t.hListener, 0)
  769. } else {
  770. t.onWSMessageHeader = call
  771. HP_Set_FN_HttpServer_OnWSMessageHeader.Call(t.hListener, server_http_callback_OnWSMessageHeader_ptr)
  772. }
  773. return t
  774. }
  775. // OnWSMessageBody
  776. //
  777. // @Description: 【可选】绑定WebSocket 数据包体事件
  778. // @param call->return HR_ 开头常量
  779. func (t *HttpServer) OnWSMessageBody(call func(t *HttpServer, Sender, ConnID, Data uintptr, Length uintptr) HR) *HttpServer {
  780. if !t.isValid() {
  781. return t
  782. }
  783. if call == nil {
  784. HP_Set_FN_HttpServer_OnWSMessageBody.Call(t.hListener, 0)
  785. } else {
  786. t.onWSMessageBody = call
  787. HP_Set_FN_HttpServer_OnWSMessageBody.Call(t.hListener, server_http_callback_OnWSMessageBody_ptr)
  788. }
  789. return t
  790. }
  791. // OnWSMessageComplete
  792. //
  793. // @Description: 【可选】绑定WebSocket 数据包完成事件
  794. // @param call->return HR_ 开头常量
  795. func (t *HttpServer) OnWSMessageComplete(call func(t *HttpServer, Sender, ConnID uintptr) HR) *HttpServer {
  796. if !t.isValid() {
  797. return t
  798. }
  799. if call == nil {
  800. HP_Set_FN_HttpServer_OnWSMessageComplete.Call(t.hListener, 0)
  801. } else {
  802. t.onWSMessageComplete = call
  803. HP_Set_FN_HttpServer_OnWSMessageComplete.Call(t.hListener, server_http_callback_OnWSMessageComplete_ptr)
  804. }
  805. return t
  806. }
  807. // OnPrepareListen
  808. //
  809. // @Description: 【可选】绑定准备监听事件
  810. // @param call->return HR_ 开头常量
  811. func (t *HttpServer) OnPrepareListen(call func(t *HttpServer, Sender, Listen uintptr) HR) *HttpServer {
  812. if !t.isValid() {
  813. return t
  814. }
  815. if call == nil {
  816. HP_Set_FN_HttpServer_OnPrepareListen.Call(t.hListener, 0)
  817. } else {
  818. t.onPrepareListen = call
  819. HP_Set_FN_HttpServer_OnPrepareListen.Call(t.hListener, server_callback_OnPrepareListen_ptr)
  820. }
  821. return t
  822. }
  823. // OnAccept
  824. //
  825. // @Description: 【可选】绑定接受事件
  826. // @param call->return HR_ 开头常量
  827. func (t *HttpServer) OnAccept(call func(t *HttpServer, Sender, ConnID, Client uintptr) HR) *HttpServer {
  828. if !t.isValid() {
  829. return t
  830. }
  831. if call == nil {
  832. HP_Set_FN_HttpServer_OnAccept.Call(t.hListener, 0)
  833. } else {
  834. t.onAccept = call
  835. HP_Set_FN_HttpServer_OnAccept.Call(t.hListener, server_callback_OnAccept_ptr)
  836. }
  837. return t
  838. }
  839. // OnHandShake
  840. //
  841. // @Description: 【可选】绑定握手事件
  842. // @param call->return HR_ 开头常量
  843. func (t *HttpServer) OnHandShake(call func(t *HttpServer, Sender, ConnID uintptr) HR) *HttpServer {
  844. if !t.isValid() {
  845. return t
  846. }
  847. if call == nil {
  848. HP_Set_FN_HttpServer_OnHandShake.Call(t.hListener, 0)
  849. } else {
  850. t.onHandShake = call
  851. HP_Set_FN_HttpServer_OnHandShake.Call(t.hListener, server_callback_OnHandShake_ptr)
  852. }
  853. return t
  854. }
  855. // OnSend
  856. //
  857. // @Description: 【可选】绑定发送事件
  858. // @param call->return HR_ 开头常量
  859. func (t *HttpServer) OnSend(call func(t *HttpServer, Sender, ConnID, Data uintptr, Length uintptr) HR) *HttpServer {
  860. if !t.isValid() {
  861. return t
  862. }
  863. if call == nil {
  864. HP_Set_FN_HttpServer_OnSend.Call(t.hListener, 0)
  865. } else {
  866. t.onSend = call
  867. HP_Set_FN_HttpServer_OnSend.Call(t.hListener, server_callback_OnSend_ptr)
  868. }
  869. return t
  870. }
  871. // OnReceive
  872. //
  873. // @Description: 【必选】绑定数据接收事件
  874. // @param call->return HR_ 开头常量
  875. func (t *HttpServer) OnReceive(call func(t *HttpServer, Sender, ConnID, Data uintptr, Length uintptr) HR) *HttpServer {
  876. if !t.isValid() {
  877. return t
  878. }
  879. if call == nil {
  880. HP_Set_FN_HttpServer_OnReceive.Call(t.hListener, 0)
  881. } else {
  882. t.onReceive = call
  883. HP_Set_FN_HttpServer_OnReceive.Call(t.hListener, server_callback_OnReceive_ptr)
  884. }
  885. return t
  886. }
  887. // OnClose
  888. //
  889. // @Description: 【必选】绑定断开事件
  890. // @param call->Operation 通过该参数标识是哪种操作导致的错误 SO_ 开头常量
  891. // @param call->return HR_ 开头常量
  892. func (t *HttpServer) OnClose(call func(t *HttpServer, Sender, ConnID uintptr, Operation SO, ErrorCode int) HR) *HttpServer {
  893. if !t.isValid() {
  894. return t
  895. }
  896. if call == nil {
  897. HP_Set_FN_HttpServer_OnClose.Call(t.hListener, 0)
  898. } else {
  899. t.onClose = call
  900. HP_Set_FN_HttpServer_OnClose.Call(t.hListener, server_callback_OnClose_ptr)
  901. }
  902. return t
  903. }
  904. // OnShutdown
  905. //
  906. // @Description: 【可选】绑定关闭事件
  907. // @param call->return HR_ 开头常量
  908. func (t *HttpServer) OnShutdown(call func(t *HttpServer, Sender uintptr) HR) *HttpServer {
  909. if !t.isValid() {
  910. return t
  911. }
  912. if call == nil {
  913. HP_Set_FN_HttpServer_OnShutdown.Call(t.hListener, 0)
  914. } else {
  915. t.onShutdown = call
  916. HP_Set_FN_HttpServer_OnShutdown.Call(t.hListener, server_callback_OnShutdown_ptr)
  917. }
  918. return t
  919. }
  920. // OnDefault
  921. //
  922. // @Description: 【可选】绑定所有【必选】事件
  923. func (t *HttpServer) OnDefault() *HttpServer {
  924. if !t.isValid() {
  925. return t
  926. }
  927. if t.onHeadersComplete == nil {
  928. t.OnHeadersComplete(func(t *HttpServer, Sender, ConnID uintptr) HPR {
  929. return HPR_OK
  930. })
  931. }
  932. if t.onBody == nil {
  933. t.OnBody(func(t *HttpServer, Sender, ConnID, Data uintptr, Length uintptr) HPR {
  934. return HPR_OK
  935. })
  936. }
  937. if t.onMessageComplete == nil {
  938. t.OnMessageComplete(func(t *HttpServer, Sender, ConnID uintptr) HPR {
  939. return HPR_OK
  940. })
  941. }
  942. if t.onParseError == nil {
  943. t.OnParseError(func(t *HttpServer, Sender, ConnID uintptr, ErrorCode int, ErrorDesc string) HPR {
  944. return HPR_OK
  945. })
  946. }
  947. if t.onReceive == nil {
  948. t.OnReceive(func(t *HttpServer, Sender, ConnID, Data uintptr, Length uintptr) HR {
  949. return HR_OK
  950. })
  951. }
  952. if t.onClose == nil {
  953. t.OnClose(func(t *HttpServer, Sender, ConnID uintptr, Operation SO, ErrorCode int) HR {
  954. return HR_OK
  955. })
  956. }
  957. return t
  958. }
  959. // --------------------------------------------------------------------------------------------------------------[TNVPairs]
  960. // TNVPair 描述:字符串名值对结构体
  961. type TNVPair struct {
  962. Name string
  963. Value string
  964. }