Esempio n. 1
0
func fileHandler(w http.ResponseWriter, r *http.Request) {
	// Determine the virtual host
	server, err := findServer(r.Host)
	if err != nil {
		errorHandler(w, r, err.String())
		return
	}
	// log.Println("URI is ", "/_static" + r.URL.Path)
	// Parse the URI, prepend "/_static"
	uri, ok := lightwave.NewURI("/_static" + r.URL.Path)
	if !ok {
		errorHandler(w, r, "Error parsing URI")
		return
	}
	// GET handler
	if r.Method != "GET" {
		errorHandler(w, r, "Unsupported HTTP method")
	}

	// Requests for the application page are redirected to the login page
	if r.URL.Path == server.Config.MainPage {
		// Find the session
		_, err := server.SessionDatabase.FindSession(r)
		if err != nil {
			http.Redirect(w, r, server.Config.LoginPage, 303)
			return
		}
	}

	ch := make(chan bool)
	req := &lightwave.GetRequest{lightwave.Request{w, r.URL.RawQuery, ch, lightwave.ClientOrigin, uri, nil}}
	server.Get(req)
	<-ch
}
Esempio n. 2
0
func clientHandler(w http.ResponseWriter, r *http.Request) {
	// Determine the virtual host
	server, err := findServer(r.Host)
	if err != nil {
		errorHandler(w, r, err.String())
		return
	}
	// Find the session
	session, err := server.SessionDatabase.FindSession(r)
	if err != nil {
		errorHandler(w, r, err.String())
		return
	}
	// Parse the URI, strip off the "/client" in front
	uri, ok := lightwave.NewURI(r.URL.Path[7:])
	if !ok {
		errorHandler(w, r, "Error parsing URI")
		return
	}
	// GET handler
	if r.Method == "GET" {
		ch := make(chan bool)
		req := &lightwave.GetRequest{lightwave.Request{w, r.URL.RawQuery, ch, lightwave.ClientOrigin, uri, session}}
		server.Get(req)
		<-ch
		log.Println("GET finished ", r.URL)
		// POST handler
	} else if r.Method == "POST" {
		// TODO: Meaningful limit on the size of data
		if r.ContentLength < 0 || r.ContentLength > 100000 {
			errorHandler(w, r, "Negative or oversize content length in HTTP POST body")
			return
		}
		buffer := make([]byte, r.ContentLength)
		_, err := r.Body.Read(buffer)
		if err != nil {
			errorHandler(w, r, "Error reading from HTTP POST body")
			return
		}
		ch := make(chan bool)
		req := &lightwave.PostRequest{lightwave.Request{w, r.URL.RawQuery, ch, lightwave.ClientOrigin, uri, session}, buffer, ""}
		if req.MimeType, ok = r.Header["Content-Type"]; !ok {
			errorHandler(w, r, "Content-Type is missing in POST")
			return
		}
		server.Post(req)
		<-ch
		log.Println("POST finished ", r.URL)
	} else {
		errorHandler(w, r, "Unsupported HTTP method")
	}
}