Newer
Older
httpcat / src / parsing.go
/**
  Author: Mark George <mark.george@otago.ac.nz>
  License: Zero-Clause BSD License
*/

package main

import (
	"errors"
	"strconv"
	"strings"
)

var (
	headers      map[string]string = make(map[string]string)
	bodies       map[string]string = make(map[string]string)
	contentTypes map[string]string = make(map[string]string)
	statuses     map[string]int    = make(map[string]int)
)

func parseHeader(header string) (string, string, error) {

	parts := strings.Split(header, ":")

	if len(parts) < 2 {
		return "", "", errors.New("header is not in the correct format")
	} else {
		name := strings.TrimSpace(parts[0])
		value := strings.TrimSpace(parts[1])

		// value had a ':' in it (probably a URL) so append the remaining part
		if len(parts) == 3 {
			value = value + ":" + strings.TrimSpace(parts[2])
		}

		headers[name] = value

		return name, value, nil
	}

}

func parseRoute(route string) (string, error) {

	parts := strings.Split(route, "|")

	if len(parts) != 4 {
		return "", errors.New("route is not in the correct format")
	} else {
		path := parts[0]
		body := parts[1]
		contentType := parts[2]
		status := parts[3]
		code, err := strconv.Atoi(status)
		if err != nil {
			return "", errors.New("route is not in the correct format")
		}

		bodies[path] = body
		statuses[path] = code
		contentTypes[path] = contentType

		return path, nil
	}

}