@@ -315,7 +315,7 @@ func (c *conn) readLoop(connID uint) {
315
315
if rErr != nil {
316
316
err = wErr
317
317
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 " )
319
319
}
320
320
return
321
321
}
@@ -327,7 +327,7 @@ func (c *conn) readLoop(connID uint) {
327
327
}
328
328
if wErr != nil {
329
329
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 " )
331
331
}
332
332
continue
333
333
}
@@ -387,22 +387,24 @@ func (c *conn) dial(s *service) (task *httpTask, err error) {
387
387
}
388
388
389
389
func (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 )
402
406
return
403
407
}
404
- c .processP2P (taskID , r )
405
- return
406
408
}
407
409
408
410
var task * httpTask
@@ -429,18 +431,20 @@ func (c *conn) processServiceData(connID uint, taskID uint32, s *service, r *buf
429
431
c .tasksRWMtx .Unlock ()
430
432
go task .process (connID , taskID , c )
431
433
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 :
438
444
writeErr = err
445
+ default :
446
+ readErr = err
439
447
}
440
- case * bufio.WriteErr :
441
- writeErr = err
442
- default :
443
- readErr = err
444
448
}
445
449
}
446
450
if task .service .LocalTimeout .Duration > 0 {
0 commit comments