85 lines
2.0 KiB
Go
85 lines
2.0 KiB
Go
package responder
|
|
|
|
import(
|
|
"strings"
|
|
"strconv"
|
|
"errors"
|
|
"bufio"
|
|
"time"
|
|
"net"
|
|
"fmt"
|
|
"os"
|
|
)
|
|
|
|
const error404StatusEnd = "HTTP/1.1 404 Not Found\r\n\r\n"
|
|
const error400StatusEnd = "HTTP/1.1 400 Bad Request\r\n\r\n"
|
|
|
|
func handler(conn net.Conn, docUrl string, codeVal string) (ret bool) {
|
|
defer func() {
|
|
if r := recover(); r != nil {
|
|
conn.Close()
|
|
ret = false
|
|
return
|
|
}
|
|
}()
|
|
defer conn.Close()
|
|
contentLen := strconv.Itoa(len(codeVal))
|
|
timeout := 11 * time.Second
|
|
conn.SetReadDeadline(time.Now().Add(timeout))
|
|
c := bufio.NewReader(conn)
|
|
line, _, _ := c.ReadLine()
|
|
|
|
srcFields := strings.Split(string(line), " ")
|
|
if len(srcFields) < 3 {
|
|
conn.Write([]byte(error400StatusEnd))
|
|
return false
|
|
}
|
|
if srcFields[0] != "GET" {
|
|
conn.Write([]byte(error400StatusEnd))
|
|
return false
|
|
}
|
|
if srcFields[2] != "HTTP/1.1" {
|
|
conn.Write([]byte(error400StatusEnd))
|
|
return false
|
|
}
|
|
if srcFields[1] == docUrl {
|
|
retData := []byte("HTTP/1.1 200 OK\r\n")
|
|
retData = append(retData, []byte("Server: tci-certrobot\r\n")[0:]...)
|
|
retData = append(retData, []byte("Content-Type: text/html\r\n")[0:]...)
|
|
retData = append(retData, []byte("Content-Length: " + contentLen)[0:]...)
|
|
retData = append(retData, []byte("\r\nConnection: close\r\n\r\n")[0:]...)
|
|
conn.Write(retData)
|
|
conn.Write([]byte(codeVal))
|
|
return true
|
|
}
|
|
conn.Write([]byte(error404StatusEnd))
|
|
return false
|
|
}
|
|
|
|
func SpinServer(c chan int, portnum string, documentUrl string, code string) bool{
|
|
l, err := net.Listen("tcp", portnum)
|
|
if err != nil {
|
|
fmt.Fprintf(os.Stderr, "Error listening:", err.Error())
|
|
return false
|
|
}
|
|
defer l.Close()
|
|
go func(){
|
|
<- c
|
|
fmt.Fprintf(os.Stderr, "Closing!\n")
|
|
l.Close()
|
|
}()
|
|
for {
|
|
conn, err := l.Accept()
|
|
if err != nil { // TODO: нужна обработка закрытия "сокета".
|
|
if errors.Is(err, net.ErrClosed) {
|
|
return true
|
|
}
|
|
fmt.Fprintf(os.Stderr, "FATAL: Accept error!\n", err.Error())
|
|
return false
|
|
}
|
|
go handler(conn, documentUrl, code)
|
|
}
|
|
|
|
return true
|
|
}
|