~ubuntu-branches/ubuntu/saucy/juju-core/saucy-proposed

« back to all changes in this revision

Viewing changes to src/code.google.com/p/go.net/websocket/hybi.go

  • Committer: Package Import Robot
  • Author(s): James Page
  • Date: 2013-07-11 17:18:27 UTC
  • mfrom: (1.1.1)
  • Revision ID: package-import@ubuntu.com-20130711171827-vjqkg40r0dlf7ys2
Tags: 1.11.2-0ubuntu1
* New upstream release.
* Make juju-core the default juju (LP: #1190634):
  - d/control: Add virtual package juju -> juju-core.
  - d/juju-core.postinst.in: Bump priority of alternatives over that of
    python juju packages.
* Enable for all architectures (LP: #1172505):
  - d/control: Version BD on golang-go to >= 2:1.1.1 to ensure CGO
    support for non-x86 archs, make juju-core Arch: any.
  - d/README.source: Dropped - no longer required.
* d/watch: Updated for new upstream tarball naming.

Show diffs side-by-side

added added

removed removed

Lines of Context:
46
46
        ErrBadClosingStatus      = &ProtocolError{"bad closing status"}
47
47
        ErrUnsupportedExtensions = &ProtocolError{"unsupported extensions"}
48
48
        ErrNotImplemented        = &ProtocolError{"not implemented"}
 
49
 
 
50
        handshakeHeader = map[string]bool{
 
51
                "Host":                   true,
 
52
                "Upgrade":                true,
 
53
                "Connection":             true,
 
54
                "Sec-Websocket-Key":      true,
 
55
                "Sec-Websocket-Origin":   true,
 
56
                "Sec-Websocket-Version":  true,
 
57
                "Sec-Websocket-Protocol": true,
 
58
                "Sec-Websocket-Accept":   true,
 
59
        }
49
60
)
50
61
 
51
62
// A hybiFrameHeader is a frame header as defined in hybi draft.
216
227
                }
217
228
                header = append(header, frame.header.MaskingKey...)
218
229
                frame.writer.Write(header)
219
 
                var data []byte
220
 
 
221
 
                for i := 0; i < length; i++ {
222
 
                        data = append(data, msg[i]^frame.header.MaskingKey[i%4])
 
230
                data := make([]byte, length)
 
231
                for i := range data {
 
232
                        data[i] = msg[i] ^ frame.header.MaskingKey[i%4]
223
233
                }
224
234
                frame.writer.Write(data)
225
235
                err = frame.writer.Flush()
409
419
        if len(config.Protocol) > 0 {
410
420
                bw.WriteString("Sec-WebSocket-Protocol: " + strings.Join(config.Protocol, ", ") + "\r\n")
411
421
        }
412
 
        // TODO(ukai): send extensions.
413
 
        // TODO(ukai): send cookie if any.
 
422
        // TODO(ukai): send Sec-WebSocket-Extensions.
 
423
        err = config.Header.WriteSubset(bw, handshakeHeader)
 
424
        if err != nil {
 
425
                return err
 
426
        }
414
427
 
415
428
        bw.WriteString("\r\n")
416
429
        if err = bw.Flush(); err != nil {
484
497
                return http.StatusBadRequest, ErrChallengeResponse
485
498
        }
486
499
        version := req.Header.Get("Sec-Websocket-Version")
487
 
        var origin string
488
500
        switch version {
489
501
        case "13":
490
502
                c.Version = ProtocolVersionHybi13
491
 
                origin = req.Header.Get("Origin")
492
503
        case "8":
493
504
                c.Version = ProtocolVersionHybi08
494
 
                origin = req.Header.Get("Sec-Websocket-Origin")
495
505
        default:
496
506
                return http.StatusBadRequest, ErrBadWebSocketVersion
497
507
        }
498
 
        c.Origin, err = url.ParseRequestURI(origin)
499
 
        if err != nil {
500
 
                return http.StatusForbidden, err
501
 
        }
502
508
        var scheme string
503
509
        if req.TLS != nil {
504
510
                scheme = "wss"
510
516
                return http.StatusBadRequest, err
511
517
        }
512
518
        protocol := strings.TrimSpace(req.Header.Get("Sec-Websocket-Protocol"))
513
 
        protocols := strings.Split(protocol, ",")
514
 
        for i := 0; i < len(protocols); i++ {
515
 
                c.Protocol = append(c.Protocol, strings.TrimSpace(protocols[i]))
 
519
        if protocol != "" {
 
520
                protocols := strings.Split(protocol, ",")
 
521
                for i := 0; i < len(protocols); i++ {
 
522
                        c.Protocol = append(c.Protocol, strings.TrimSpace(protocols[i]))
 
523
                }
516
524
        }
517
525
        c.accept, err = getNonceAccept([]byte(key))
518
526
        if err != nil {
521
529
        return http.StatusSwitchingProtocols, nil
522
530
}
523
531
 
 
532
// Origin parses Origin header in "req".
 
533
// If origin is "null", returns (nil, nil).
 
534
func Origin(config *Config, req *http.Request) (*url.URL, error) {
 
535
        var origin string
 
536
        switch config.Version {
 
537
        case ProtocolVersionHybi13:
 
538
                origin = req.Header.Get("Origin")
 
539
        case ProtocolVersionHybi08:
 
540
                origin = req.Header.Get("Sec-Websocket-Origin")
 
541
        }
 
542
        if origin == "null" {
 
543
                return nil, nil
 
544
        }
 
545
        return url.ParseRequestURI(origin)
 
546
}
 
547
 
524
548
func (c *hybiServerHandshaker) AcceptHandshake(buf *bufio.Writer) (err error) {
525
549
        if len(c.Protocol) > 0 {
526
550
                if len(c.Protocol) != 1 {
 
551
                        // You need choose a Protocol in Handshake func in Server.
527
552
                        return ErrBadWebSocketProtocol
528
553
                }
529
554
        }
534
559
        if len(c.Protocol) > 0 {
535
560
                buf.WriteString("Sec-WebSocket-Protocol: " + c.Protocol[0] + "\r\n")
536
561
        }
537
 
        // TODO(ukai): support extensions
 
562
        // TODO(ukai): send Sec-WebSocket-Extensions.
 
563
        if c.Header != nil {
 
564
                err := c.Header.WriteSubset(buf, handshakeHeader)
 
565
                if err != nil {
 
566
                        return err
 
567
                }
 
568
        }
538
569
        buf.WriteString("\r\n")
539
570
        return buf.Flush()
540
571
}