github.com/cloud-foundations/dominator@v0.0.0-20221004181915-6e4fee580046/hypervisor/httpd/showBootLog.go (about)

     1  package httpd
     2  
     3  import (
     4  	"bufio"
     5  	"net"
     6  	"net/http"
     7  
     8  	"github.com/Cloud-Foundations/Dominator/lib/url"
     9  )
    10  
    11  func (s state) showBootLogHandler(w http.ResponseWriter, req *http.Request) {
    12  	parsedQuery := url.ParseQuery(req.URL)
    13  	if len(parsedQuery.Flags) != 1 {
    14  		w.Header().Set("Content-Type", "text/plain; charset=utf-8")
    15  		w.WriteHeader(http.StatusBadRequest)
    16  		return
    17  	}
    18  	var ipAddr string
    19  	for name := range parsedQuery.Flags {
    20  		ipAddr = name
    21  	}
    22  	r, err := s.manager.GetVmBootLog(net.ParseIP(ipAddr))
    23  	if err != nil {
    24  		w.Header().Set("Content-Type", "text/plain; charset=utf-8")
    25  		w.WriteHeader(http.StatusNotFound)
    26  		return
    27  	}
    28  	defer r.Close()
    29  	reader := bufio.NewReader(r)
    30  	writer := bufio.NewWriter(w)
    31  	defer writer.Flush()
    32  	// Strip out useless and annoying CRLF and replace with LF only.
    33  	for {
    34  		line, err := reader.ReadBytes('\n')
    35  		if len(line) > 1 {
    36  			if line[len(line)-1] == '\n' && line[len(line)-2] == '\r' {
    37  				line[len(line)-2] = '\n'
    38  				line = line[:len(line)-1]
    39  			}
    40  			writer.Write(line)
    41  		}
    42  		if err != nil {
    43  			return
    44  		}
    45  	}
    46  }