Skip to content

Commit

Permalink
use Go naming conventions
Browse files Browse the repository at this point in the history
  • Loading branch information
jakecoffman committed Sep 17, 2019
1 parent c6f3d0f commit 6f20127
Show file tree
Hide file tree
Showing 10 changed files with 87 additions and 79 deletions.
2 changes: 1 addition & 1 deletion cmd/guac/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func main() {

fs := http.FileServer(http.Dir("."))

servlet := guac.NewHTTPTunnelServlet(DemoDoConnect)
servlet := guac.NewServer(DemoDoConnect)

mux := http.NewServeMux()
mux.Handle("/tunnel", servlet)
Expand Down
8 changes: 4 additions & 4 deletions cmd/wwt/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func main() {

fs := http.FileServer(http.Dir("."))

servlet := guac.NewHTTPTunnelServlet(DemoDoConnect)
servlet := guac.NewServer(DemoDoConnect)
wsServer := guac.NewWebsocketServer(DemoDoConnect)

mux := http.NewServeMux()
Expand Down Expand Up @@ -104,18 +104,18 @@ func DemoDoConnect(request *http.Request) (guac.Tunnel, error) {
info.AudioMimetypes = []string{"audio/L16", "rate=44100", "channels=2"}

logrus.Debug("Connecting to guacd")
socket, err := guac.NewInetSocket("127.0.0.1", 4822)
stream, err := guac.NewInetSocket("127.0.0.1", 4822)
if err != nil {
return nil, err
}
logrus.Debug("Connected to guacd")
if request.URL.Query().Get("uuid") != "" {
config.ConnectionID = request.URL.Query().Get("uuid")
}
err = guac.ConfigureSocket(socket, config, info)
err = guac.ConfigureSocket(stream, config, info)
if err != nil {
return nil, err
}
logrus.Debug("Socket configured")
return guac.NewSimpleTunnel(socket), nil
return guac.NewSimpleTunnel(stream), nil
}
File renamed without changes.
32 changes: 16 additions & 16 deletions httpTunnelServer.go → server.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,16 @@ const (
uuidLength = 36
)

// HttpTunnelServer uses HTTP requests to talk to guacd
type HttpTunnelServer struct {
tunnels HttpTunnelMap
// Server uses HTTP requests to talk to guacd
type Server struct {
tunnels TunnelMap
connect func(*http.Request) (Tunnel, error)
}

// NewHTTPTunnelServlet constructor
func NewHTTPTunnelServlet(connect func(r *http.Request) (Tunnel, error)) *HttpTunnelServer {
return &HttpTunnelServer{
tunnels: NewHttpTunnelMap(),
// NewServer constructor
func NewServer(connect func(r *http.Request) (Tunnel, error)) *Server {
return &Server{
tunnels: NewTunnelMap(),
connect: connect,
}
}
Expand All @@ -34,7 +34,7 @@ func NewHTTPTunnelServlet(connect func(r *http.Request) (Tunnel, error)) *HttpTu
* Registers the given tunnel such that future read/write requests to that
* tunnel will be properly directed.
*/
func (s *HttpTunnelServer) registerTunnel(tunnel Tunnel) {
func (s *Server) registerTunnel(tunnel Tunnel) {
s.tunnels.Put(tunnel.GetUUID(), tunnel)
logger.Debugf("Registered tunnel \"%v\".", tunnel.GetUUID())
}
Expand All @@ -43,7 +43,7 @@ func (s *HttpTunnelServer) registerTunnel(tunnel Tunnel) {
* Deregisters the given tunnel such that future read/write requests to
* that tunnel will be rejected.
*/
func (s *HttpTunnelServer) deregisterTunnel(tunnel Tunnel) {
func (s *Server) deregisterTunnel(tunnel Tunnel) {
s.tunnels.Remove(tunnel.GetUUID())
logger.Debugf("Deregistered tunnel \"%v\".", tunnel.GetUUID())
}
Expand All @@ -52,7 +52,7 @@ func (s *HttpTunnelServer) deregisterTunnel(tunnel Tunnel) {
* Returns the tunnel with the given UUID, if it has been registered with
* registerTunnel() and not yet deregistered with deregisterTunnel().
*/
func (s *HttpTunnelServer) getTunnel(tunnelUUID string) (ret Tunnel, err error) {
func (s *Server) getTunnel(tunnelUUID string) (ret Tunnel, err error) {
var ok bool
ret, ok = s.tunnels.Get(tunnelUUID)

Expand All @@ -62,13 +62,13 @@ func (s *HttpTunnelServer) getTunnel(tunnelUUID string) (ret Tunnel, err error)
return
}

func (s *HttpTunnelServer) sendError(response http.ResponseWriter, guacStatus Status, message string) {
func (s *Server) sendError(response http.ResponseWriter, guacStatus Status, message string) {
response.Header().Set("Guacamole-Status-Code", fmt.Sprintf("%v", guacStatus.GetGuacamoleStatusCode()))
response.Header().Set("Guacamole-Error-Message", message)
response.WriteHeader(guacStatus.GetHTTPStatusCode())
}

func (s *HttpTunnelServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
err := s.handleTunnelRequestCore(w, r)
if err == nil {
return
Expand All @@ -86,7 +86,7 @@ func (s *HttpTunnelServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
return
}

func (s *HttpTunnelServer) handleTunnelRequestCore(response http.ResponseWriter, request *http.Request) (err error) {
func (s *Server) handleTunnelRequestCore(response http.ResponseWriter, request *http.Request) (err error) {
query := request.URL.RawQuery
if len(query) == 0 {
return ErrClient.NewError("No query string provided.")
Expand Down Expand Up @@ -133,7 +133,7 @@ func (s *HttpTunnelServer) handleTunnelRequestCore(response http.ResponseWriter,
}

// doRead takes guacd messages and sends them in the response
func (s *HttpTunnelServer) doRead(response http.ResponseWriter, request *http.Request, tunnelUUID string) error {
func (s *Server) doRead(response http.ResponseWriter, request *http.Request, tunnelUUID string) error {
tunnel, err := s.getTunnel(tunnelUUID)
if err != nil {
return err
Expand Down Expand Up @@ -181,7 +181,7 @@ func (s *HttpTunnelServer) doRead(response http.ResponseWriter, request *http.Re
}

// writeSome drains the guacd buffer holding instructions into the response
func (s *HttpTunnelServer) writeSome(response http.ResponseWriter, guacd InstructionReader, tunnel Tunnel) (err error) {
func (s *Server) writeSome(response http.ResponseWriter, guacd InstructionReader, tunnel Tunnel) (err error) {
var message []byte

for {
Expand Down Expand Up @@ -225,7 +225,7 @@ func (s *HttpTunnelServer) writeSome(response http.ResponseWriter, guacd Instruc
}

// doWrite takes data from the request and sends it to guacd
func (s *HttpTunnelServer) doWrite(response http.ResponseWriter, request *http.Request, tunnelUUID string) error {
func (s *Server) doWrite(response http.ResponseWriter, request *http.Request, tunnelUUID string) error {
tunnel, err := s.getTunnel(tunnelUUID)
if err != nil {
return err
Expand Down
4 changes: 1 addition & 3 deletions socket.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@ import (
"time"
)

// SocketTimeout stream timeout setting
// * The number of milliseconds to wait for data on the TCP stream before
// * timing out.
// SocketTimeout is the longest time a read or write from Guacamole may take
const SocketTimeout = 15 * time.Second

// NewInetSocket connects to Guacamole via non-tls dialer
Expand Down
2 changes: 1 addition & 1 deletion stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ func (s *Stream) ReadSome() (instruction []byte, err error) {
}

n, err = s.conn.Read(s.buffer[len(s.buffer):cap(s.buffer)])
if err != nil {
if err != nil && n == 0{
switch err.(type) {
case net.Error:
ex := err.(net.Error)
Expand Down
14 changes: 12 additions & 2 deletions stream_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,13 @@ func TestInstructionReader_ReadSome(t *testing.T) {
if !bytes.Equal(ins, []byte("4.copy,2.ab;")) {
t.Error("Unexpected bytes returned")
}
if !stream.Available() {
t.Error("Stream has more available but returned false")
}

// Read some more to simulate data being fragmented
copy(conn.ToRead, ",2.ab;")
// Read the rest of the fragmented instruction
n := copy(conn.ToRead, ",2.ab;")
conn.ToRead = conn.ToRead[:n]
conn.HasRead = false
ins, err = stream.ReadSome()

Expand All @@ -34,6 +38,9 @@ func TestInstructionReader_ReadSome(t *testing.T) {
if !bytes.Equal(ins, []byte("4.copy,2.ab;")) {
t.Error("Unexpected bytes returned")
}
if stream.Available() {
t.Error("Stream thinks it has more available but doesn't")
}
}

func TestInstructionReader_Flush(t *testing.T) {
Expand All @@ -50,6 +57,9 @@ func TestInstructionReader_Flush(t *testing.T) {
if s.buffer[0] != '3' && s.buffer[1] != '4' {
t.Error("Unexpected buffer contents:", string(s.buffer[:2]))
}
if len(s.buffer) != 2 {
t.Error("Unexpected length", len(s.buffer))
}
}

type fakeConn struct {
Expand Down
Loading

0 comments on commit 6f20127

Please sign in to comment.