@@ -315,7 +315,7 @@ func (c *conn) readLoop(connID uint) {
315315 if rErr != nil {
316316 err = wErr
317317 if ! errors .Is (rErr , net .ErrClosed ) {
318- c .Logger .Warn ().Err (rErr ).Msg ("failed to read data in processData " )
318+ c .Logger .Warn ().Err (rErr ).Msg ("failed to read data in processServiceData " )
319319 }
320320 return
321321 }
@@ -327,7 +327,7 @@ func (c *conn) readLoop(connID uint) {
327327 }
328328 if wErr != nil {
329329 if ! errors .Is (wErr , net .ErrClosed ) {
330- c .Logger .Warn ().Err (wErr ).Msg ("failed to write data in processData " )
330+ c .Logger .Warn ().Err (wErr ).Msg ("failed to write data in processServiceData " )
331331 }
332332 continue
333333 }
@@ -387,22 +387,24 @@ func (c *conn) dial(s *service) (task *httpTask, err error) {
387387}
388388
389389func (c * conn ) processServiceData (connID uint , taskID uint32 , s * service , r * bufio.LimitedReader ) (readErr , writeErr error ) {
390- var peekBytes []byte
391- peekBytes , readErr = r .Peek (2 )
392- if readErr != nil {
393- return
394- }
395- // first 2 bytes of p2p sdp request is "XP"(0x5850)
396- isP2P := (uint16 (peekBytes [1 ]) | uint16 (peekBytes [0 ])<< 8 ) == 0x5850
397- if isP2P {
398- if len (c .stuns ) < 1 {
399- respAndClose (taskID , c , [][]byte {
400- []byte ("HTTP/1.1 403 Forbidden\r \n Connection: Closed\r \n \r \n " ),
401- })
390+ if r .N > 0 {
391+ var peekBytes []byte
392+ peekBytes , readErr = r .Peek (2 )
393+ if readErr != nil {
394+ return
395+ }
396+ // first 2 bytes of p2p sdp request is "XP"(0x5850)
397+ isP2P := (uint16 (peekBytes [1 ]) | uint16 (peekBytes [0 ])<< 8 ) == 0x5850
398+ if isP2P {
399+ if len (c .stuns ) < 1 {
400+ respAndClose (taskID , c , [][]byte {
401+ []byte ("HTTP/1.1 403 Forbidden\r \n Connection: Closed\r \n \r \n " ),
402+ })
403+ return
404+ }
405+ c .processP2P (taskID , r )
402406 return
403407 }
404- c .processP2P (taskID , r )
405- return
406408 }
407409
408410 var task * httpTask
@@ -429,18 +431,20 @@ func (c *conn) processServiceData(connID uint, taskID uint32, s *service, r *buf
429431 c .tasksRWMtx .Unlock ()
430432 go task .process (connID , taskID , c )
431433
432- _ , err := r .WriteTo (task )
433- if err != nil {
434- switch e := err .(type ) {
435- case * net.OpError :
436- switch e .Op {
437- case "write" :
434+ if r .N > 0 {
435+ _ , err := r .WriteTo (task )
436+ if err != nil {
437+ switch e := err .(type ) {
438+ case * net.OpError :
439+ switch e .Op {
440+ case "write" :
441+ writeErr = err
442+ }
443+ case * bufio.WriteErr :
438444 writeErr = err
445+ default :
446+ readErr = err
439447 }
440- case * bufio.WriteErr :
441- writeErr = err
442- default :
443- readErr = err
444448 }
445449 }
446450 if task .service .LocalTimeout .Duration > 0 {
0 commit comments