示例#1
0
文件: http.go 项目: nrolans/netb
func (h *HTTPServer) ServeHTTP(w http.ResponseWriter, req *http.Request) {
	h.wgHandlers.Add(1)
	defer h.wgHandlers.Done()

	log.Printf("New HTTP request from %s\n", req.RemoteAddr)

	// Method check
	if req.Method != "PUT" {
		log.Printf("Received a %s method from %s, rejecting\n", req.Method, req.RemoteAddr)
		http.Error(w, "PUT method expected", 405)
		return
	}

	if req.ContentLength > h.config.FileSizeLimit {
		log.Printf("Received a file larger than allowed from %s (%d > %d)\n", req.RemoteAddr, req.ContentLength, h.config.FileSizeLimit)
		http.Error(w, "File too large", 403)
		return
	}

	// Hostname check
	name, err := lookupIP(req.RemoteAddr)
	if err != nil {
		log.Printf("Unable to lookup host %s\n", req.RemoteAddr)
		http.Error(w, "Host not known", 403)
		return
	}

	e := configstore.NewEntry()
	e.Name = name
	e.Date = time.Now()
	written, err := io.Copy(e, req.Body)
	if err != nil {
		log.Println("Failed to copy body into entry: %s", err)
		http.Error(w, "Failed to read body", 500)
	}
	req.Body.Close()

	if err := h.config.Store.Add(*e); err != nil {
		log.Println("Failed to write entry: %s", err)
		http.Error(w, "Failed to write entry", 500)
		return
	}

	log.Printf("Successfully received a file from %s (%s) - %d bytes\n", req.RemoteAddr, name, written)

	w.WriteHeader(200)
	fmt.Fprintf(w, "Saved %d bytes", written)

	return
}
示例#2
0
文件: scp.go 项目: nrolans/netb
func (h *SCPHandler) FileCopy(r io.Reader) scp.Status {
	// Prepare a new entry
	e := configstore.NewEntry()
	e.Name = h.name
	e.Date = time.Now()

	// Copy the content
	n, err := io.Copy(e, r)
	if err != nil {
		log.Printf("Error copying file to entry: %s", err)
		return *scp.NewStatus(scp.Warning, "Error copying file")
	}
	log.Printf("Copied %d bytes", n)

	err = h.store.Add(*e)
	if err != nil {
		log.Printf("Error writing entry to store: %s", err)
		return *scp.NewStatus(scp.Warning, "Error saving file")
	}

	return *scp.NewStatus(scp.OK, "")
}