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)
	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 := parts[0]
		value := parts[1]

		headers[name] = value

		return name, value, nil
	}

}

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

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

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

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

		return path, body, code, nil
	}

}