Ported http post - tested and working

This commit is contained in:
saxon 2018-07-18 13:26:23 +09:30
parent 2dd7b344f8
commit daf219c937
1 changed files with 35 additions and 27 deletions

View File

@ -289,13 +289,12 @@ func afmEncodeString(output *byte, outend *byte, bv *C.AVal) *byte {
// TODO Encode Int16
outputPtr = unsafe.Pointer(C.AMF_EncodeInt32((*C.char)(outputPtr), (*C.char)(outendPtr), bv.av_len))
}
C.memcpy(unsafe.Pointer(outputPtr), unsafe.Pointer(bv.av_val), (C.ulong)(bv.av_len))
C.memcpy(unsafe.Pointer(outputPtr), unsafe.Pointer(bv.av_val), (C.size_t)(bv.av_len))
incBytePtr(outputPtr, int(bv.av_len))
return (*byte)(outputPtr)
}
// send packet version 1 - less C stuff
func sendPacket(r *C.RTMP, packet *C.RTMPPacket, queue int) int {
var prevPacket *C.RTMPPacket
@ -574,7 +573,7 @@ func writeN(r *C.RTMP, buffer unsafe.Pointer, n int) int {
if (r.Link.protocol & C.RTMP_FEATURE_HTTP) != 0 {
// TODO: port HTTP_POST
nBytes = int(C.HTTP_Post(r, RTMPT_SEND, (*C.char)(ptr), C.int(n)))
nBytes = httpPost(r, RTMPT_SEND, (*byte)(ptr), n)
} else {
// TODO: port this if necessary
nBytes = int(C.RTMPSockBuf_Send(&r.m_sb, (*C.char)(ptr), C.int(n)))
@ -605,49 +604,58 @@ func writeN(r *C.RTMP, buffer unsafe.Pointer, n int) int {
return 0
}
func httpPost(r *C.RTMP, cmd RTMPTCmd, buf *byte, len int) int {
length := 512
var hbuf [length]byte
fString := fmt.Sprintf("POST /%s%s/%d HTTP/1.1\r\n"
"Host: %.*s:%d\r\n"
"Accept: */*\r\n"
"User-Agent: Shockwave Flash\r\n"
"Connection: Keep-Alive\r\n"
"Cache-Control: no-cache\r\n"
"Content-type: application/x-fcs\r\n"
"Content-length: %d\r\n\r\n", RTMPT_cmds[cmd],
r->m_clientID.av_val ? r->m_clientID.av_val : "",
r->m_msgCounter, r->Link.hostname.av_len, r->Link.hostname.av_val,
r->Link.port, len)
const length = 512
var RTMPT_cmds = []string{
"open",
"send",
"idle",
"close",
}
func httpPost(r *C.RTMP, cmd C.RTMPTCmd, buf *byte, l int) int {
res := ""
if r.m_clientID.av_val != nil {
res = string(ptrToSlice(unsafe.Pointer(r.m_clientID.av_val),
int(r.m_clientID.av_len)))
}
fString := fmt.Sprintf("POST /%s%s/%d HTTP/1.1\r\n"+
"Host: %v:%d\r\n"+
"Accept: */*\r\n"+
"User-Agent: Shockwave Flash\r\n"+
"Connection: Keep-Alive\r\n"+
"Cache-Control: no-cache\r\n"+
"Content-type: application/x-fcs\r\n"+
"Content-length: %d\r\n\r\n", RTMPT_cmds[cmd], res, r.m_msgCounter,
r.Link.hostname.av_len, r.Link.hostname.av_val, r.Link.port, l)
hlen := len(fString)
hbuf = (*byte)(unsafe.Pointer(&(([]byte{fString})[0])))
hbuf := (*byte)(unsafe.Pointer(&(([]byte(fString))[0])))
// TODO: port this
C.RTMPSockBuf_Send(&r.m_sb, (*C.char)(hbuf), hlen)
hlen = c.RTMPSockBuf_Send(&r.m_sb, buf, len)
C.RTMPSockBuf_Send(&r.m_sb, (*C.char)(unsafe.Pointer(hbuf)), C.int(hlen))
hlen = int(C.RTMPSockBuf_Send(&r.m_sb, (*C.char)(unsafe.Pointer(buf)), C.int(l)))
r.m_msgCounter++
r.m_unackd++
return hlen
}
// TODO: port RTMP_METHOD
func avQueue(vals **C.RTMP_METHOD, num *int, av *C.AVal, txn int ) {
func avQueue(vals **C.RTMP_METHOD, num *int, av *C.AVal, txn int) {
var rtmpMethodPtr *C.RTMP_METHOD
if (*num & 0x0f) == 0 {
// TODO: work out what to do with the realloc
*vals = (*C.RTMP_METHOD)(C.realloc(unsafe.Pointer(*vals), C.size_t((*num+16) * int(unsafe.Sizeof(*(*vals))))))
*vals = (*C.RTMP_METHOD)(C.realloc(unsafe.Pointer(*vals), C.size_t((*num+16)*int(unsafe.Sizeof(*(*vals))))))
}
tmp := unsafe.Pointer(C.malloc(C.size_t(av.av_len + 1)))
C.memcpy(tmp, unsafe.Pointer(av.av_val), C.size_t(av.av_len))
*indxBytePtr(tmp,int(av.av_len)) = *(*byte)(unsafe.Pointer(C.CString("")))
(*(*C.RTMP_METHOD)(incPtr(unsafe.Pointer(*vals),*num,
*indxBytePtr(tmp, int(av.av_len)) = *(*byte)(unsafe.Pointer(C.CString("")))
(*(*C.RTMP_METHOD)(incPtr(unsafe.Pointer(*vals), *num,
int(unsafe.Sizeof(rtmpMethodPtr))))).num = C.int(txn)
(*(*C.RTMP_METHOD)(incPtr(unsafe.Pointer(*vals),*num,
(*(*C.RTMP_METHOD)(incPtr(unsafe.Pointer(*vals), *num,
int(unsafe.Sizeof(rtmpMethodPtr))))).name.av_len = av.av_len
(*(*C.RTMP_METHOD)(incPtr(unsafe.Pointer(*vals),*num,
(*(*C.RTMP_METHOD)(incPtr(unsafe.Pointer(*vals), *num,
int(unsafe.Sizeof(rtmpMethodPtr))))).name.av_val = (*C.char)(tmp)
}
// indxBytePtr returns a byte at the indx inc give a ptr
func indxBytePtr(ptr unsafe.Pointer, inc int) *byte {
return (*byte)(incPtr(ptr, inc, byteSize))