github.com/moontrade/nogc@v0.1.7/net/http/parser.go (about) 1 package httpparser 2 3 /* 4 #include "picohttpparser.h" 5 #include <stdlib.h> 6 7 typedef struct { 8 uintptr_t buf; 9 uintptr_t len; 10 uintptr_t method; 11 size_t method_len; 12 uintptr_t path; 13 size_t path_len; 14 uintptr_t headers; 15 size_t num_headers; 16 size_t max_num_headers; 17 uintptr_t last_len; 18 int minor_version; 19 int result; 20 } phr_parse_request_t; 21 22 void do_phr_parse_request(size_t arg0, size_t arg1) { 23 phr_parse_request_t* args = (phr_parse_request_t*)(void*)arg0; 24 25 const char *method; 26 const char *path; 27 args->result = phr_parse_request( 28 (const char*)args->buf, 29 args->len, 30 &method, 31 &args->method_len, 32 &path, 33 &args->path_len, 34 &args->minor_version, 35 (struct phr_header*)args->headers, 36 &args->num_headers, 37 args->last_len 38 ); 39 args->method = (uintptr_t)method; 40 args->path = (uintptr_t)path; 41 } 42 43 44 */ 45 import "C" 46 import ( 47 "github.com/moontrade/nogc" 48 "github.com/moontrade/nogc/unsafecgo" 49 "reflect" 50 "unsafe" 51 ) 52 53 type Request struct { 54 input uintptr 55 inputLen uintptr 56 method uintptr 57 methodLen uintptr 58 path uintptr 59 pathLen uintptr 60 headers uintptr 61 numHeaders uintptr 62 maxHeaders uintptr 63 lastLen uintptr 64 minorVersion int32 65 result int32 66 } 67 68 func AllocRequest(headers int) *Request { 69 if headers < 10 { 70 headers = 10 71 } 72 if headers > 4096 { 73 headers = 4096 74 } 75 p := nogc.AllocZeroed(unsafe.Sizeof(Request{}) + (unsafe.Sizeof(Header{}) * uintptr(headers))) 76 r := (*Request)(unsafe.Pointer(p)) 77 r.headers = uintptr(p.Add(int(unsafe.Sizeof(Request{})))) 78 r.numHeaders = uintptr(headers) 79 r.maxHeaders = uintptr(headers) 80 return r 81 } 82 83 func (p *Request) Free() { 84 nogc.Free(nogc.Pointer(unsafe.Pointer(p))) 85 } 86 87 func (p *Request) Method() string { 88 return *(*string)(unsafe.Pointer(&reflect.StringHeader{ 89 Data: p.method, 90 Len: int(p.methodLen), 91 })) 92 } 93 func (p *Request) Path() string { 94 return *(*string)(unsafe.Pointer(&reflect.StringHeader{ 95 Data: p.path, 96 Len: int(p.pathLen), 97 })) 98 } 99 100 func (p *Request) NumHeaders() int { 101 return int(p.numHeaders) 102 } 103 104 func (p *Request) Header(index int) *Header { 105 return (*Header)(unsafe.Pointer(uintptr(unsafe.Pointer(p)) + unsafe.Sizeof(Request{}) + (unsafe.Sizeof(Header{}) * uintptr(index)))) 106 } 107 108 type Header struct { 109 Name nogc.FatPointer 110 Value nogc.FatPointer 111 } 112 113 //func printSizeOfs() { 114 // type SizeT C.size_t 115 // type UintptrT C.uintptr_t 116 // type Int C.int 117 // println("sizeof(size_t)", uint(unsafe.Sizeof(SizeT(0))), 118 // "sizeof(uintptr_t)", uint(unsafe.Sizeof(UintptrT(0))), 119 // "sizeof(struct phr_header)", uint(unsafe.Sizeof(Header{})), 120 // "sizeof(int)", uint(unsafe.Sizeof(Int(0)))) 121 //} 122 123 func ParseRequest(args *Request) int { 124 //args := Request{} 125 args.numHeaders = args.maxHeaders 126 ptr := uintptr(unsafe.Pointer(args)) 127 unsafecgo.NonBlocking((*byte)(C.do_phr_parse_request), ptr, 0) 128 return int(args.result) 129 }