rtmp: simplify url parsing

This commit is contained in:
Dan Kortschak 2018-09-07 07:01:59 +09:30
parent bf2a2ec7a8
commit 9212bed0ed
2 changed files with 21 additions and 30 deletions

View File

@ -39,17 +39,14 @@ import (
"unsafe" "unsafe"
) )
// int RTMP_ParseURL(const char *url, int *protocol, AVal *host, unsigned int *port, // int RTMP_ParseURL(const char *url, int *protocol, AVal *host, unsigned int *port, AVal *playpath, AVal *app);
// AVal *playpath, AVal *app);
// parseurl.c +33 // parseurl.c +33
func C_RTMP_ParseURL(url *byte, protocol *int32, host *C_AVal, port *uint32, func C_RTMP_ParseURL(url *byte, host *C_AVal, port *uint16, playpath, app *C_AVal) (protocol int32, ok bool) {
playpath *C_AVal, app *C_AVal) int {
var p, end, col, ques, slash *byte var p, end, col, ques, slash *byte
// TODO: use our logger here // TODO: use our logger here
// RTMP_Log(RTMP_LOGDEBUG, "Parsing..."); // RTMP_Log(RTMP_LOGDEBUG, "Parsing...");
*protocol = RTMP_PROTOCOL_RTMP protocol = RTMP_PROTOCOL_RTMP
*port = 0 *port = 0
playpath.av_len = 0 playpath.av_len = 0
playpath.av_val = nil playpath.av_val = nil
@ -61,7 +58,7 @@ func C_RTMP_ParseURL(url *byte, protocol *int32, host *C_AVal, port *uint32,
if p == nil { if p == nil {
// TODO: use our logger here // TODO: use our logger here
log.Println("RTMP URL: No :// in url!") log.Println("RTMP URL: No :// in url!")
return 0 return protocol, false
} }
/* /*
NOTE: the following code nees to be ported if we're using anything other than NOTE: the following code nees to be ported if we're using anything other than
@ -99,7 +96,7 @@ func C_RTMP_ParseURL(url *byte, protocol *int32, host *C_AVal, port *uint32,
if *p == 0 { if *p == 0 {
// TODO: use new logger here // TODO: use new logger here
// RTMP_Log(RTMP_LOGWARNING, "No hostname in URL!"); // RTMP_Log(RTMP_LOGWARNING, "No hostname in URL!");
return 0 return protocol, false
} }
end = (*byte)(unsafe.Pointer(uintptr(unsafe.Pointer(p)) + uintptr(strlen(p)))) end = (*byte)(unsafe.Pointer(uintptr(unsafe.Pointer(p)) + uintptr(strlen(p))))
@ -135,20 +132,23 @@ func C_RTMP_ParseURL(url *byte, protocol *int32, host *C_AVal, port *uint32,
if *p == ':' { if *p == ':' {
var p2 uint32 var p2 uint32
p = (*byte)(incBytePtr(unsafe.Pointer(p), 1)) p = (*byte)(incBytePtr(unsafe.Pointer(p), 1))
tmp, _ := strconv.Atoi(cStrToGoStr(p)) tmp, err := strconv.Atoi(cStrToGoStr(p))
if err != nil {
return protocol, false
}
p2 = uint32(tmp) p2 = uint32(tmp)
if p2 > 65535 { if p2 > 65535 {
// TODO: use new logger with this // TODO: use new logger with this
// RTMP_Log(RTMP_LOGWARNING, "Invalid port number!"); // RTMP_Log(RTMP_LOGWARNING, "Invalid port number!");
} else { } else {
*port = p2 *port = uint16(p2)
} }
} }
if slash == nil { if slash == nil {
// TODO: use new logger // TODO: use new logger
// RTMP_Log(RTMP_LOGWARNING, "No application or playpath in URL!"); // RTMP_Log(RTMP_LOGWARNING, "No application or playpath in URL!");
return 1 return protocol, true
} }
p = (*byte)(incBytePtr(unsafe.Pointer(slash), 1)) p = (*byte)(incBytePtr(unsafe.Pointer(slash), 1))
@ -225,7 +225,7 @@ func C_RTMP_ParseURL(url *byte, protocol *int32, host *C_AVal, port *uint32,
C_RTMP_ParsePlaypath(&av, playpath) C_RTMP_ParsePlaypath(&av, playpath)
} }
return 1 return protocol, true
} }
// void RTMP_ParsePlaypath(AVal *in, AVal *out); // void RTMP_ParsePlaypath(AVal *in, AVal *out);

View File

@ -295,34 +295,25 @@ func C_SocksSetup(r *C_RTMP, sockshost *C_AVal) {
// int RTMP_SetupURL(RTMP *r, char* url); // int RTMP_SetupURL(RTMP *r, char* url);
// rtmp.c +757 // rtmp.c +757
// NOTE: code dealing with rtmp over http has been disregarded // NOTE: code dealing with rtmp over http has been disregarded
func C_RTMP_SetupURL(r *C_RTMP, u string) (ok bool) { func C_RTMP_SetupURL(r *C_RTMP, addr string) (ok bool) {
url := goStrToCStr(u) u := goStrToCStr(addr)
length := strlen(u)
var length int32 r.Link.protocol, ok = C_RTMP_ParseURL(u, &r.Link.hostname, &r.Link.port, &r.Link.playpath0, &r.Link.app)
var port uint32 if !ok {
port = 0
length = strlen(url)
// TODO: port this
ret := int32(C_RTMP_ParseURL((*byte)(unsafe.Pointer(url)), (*int32)(
unsafe.Pointer(&r.Link.protocol)), &r.Link.hostname, (*uint32)(
unsafe.Pointer(&port)), &r.Link.playpath0, &r.Link.app))
if ret == 0 {
return false return false
} }
r.Link.port = uint16(port)
r.Link.playpath = r.Link.playpath0 r.Link.playpath = r.Link.playpath0
if r.Link.tcUrl.av_len == 0 { if r.Link.tcUrl.av_len == 0 {
r.Link.tcUrl.av_val = (*byte)(unsafe.Pointer(url)) r.Link.tcUrl.av_val = (*byte)(unsafe.Pointer(u))
if r.Link.app.av_len != 0 { if r.Link.app.av_len != 0 {
if int(uintptr(unsafe.Pointer(r.Link.app.av_val))) < if int(uintptr(unsafe.Pointer(r.Link.app.av_val))) <
int(uintptr(incBytePtr(unsafe.Pointer(url), int(length)))) { int(uintptr(incBytePtr(unsafe.Pointer(u), int(length)))) {
r.Link.tcUrl.av_len = int32(int(r.Link.app.av_len) + r.Link.tcUrl.av_len = int32(int(r.Link.app.av_len) +
int(uintptr(decBytePtr(unsafe.Pointer(r.Link.app.av_val), int(uintptr(decBytePtr(unsafe.Pointer(r.Link.app.av_val),
int(uintptr(unsafe.Pointer(url))))))) int(uintptr(unsafe.Pointer(u)))))))
} else { } else {
length = int32(r.Link.hostname.av_len) + int32(r.Link.app.av_len) + length = int32(r.Link.hostname.av_len) + int32(r.Link.app.av_len) +
int32(len("rtmpte://:65535/\x00")) int32(len("rtmpte://:65535/\x00"))
@ -343,7 +334,7 @@ func C_RTMP_SetupURL(r *C_RTMP, u string) (ok bool) {
r.Link.lFlags |= RTMP_LF_FTCU r.Link.lFlags |= RTMP_LF_FTCU
} }
} else { } else {
r.Link.tcUrl.av_len = int32(strlen(url)) r.Link.tcUrl.av_len = int32(strlen(u))
} }
} }