<commit_before>module Http.Response

import Data.Vect
import Http.RawResponse
import List.Split

%access public

record ResponseStatus where
  constructor MkResponseStatus
  responseStatusVersion : String
  responseStatusCode : Int
  responseStatusComment : String

instance Show ResponseStatus where
  show (MkResponseStatus ver code cmt) =
    "MkResponseStatus " ++ ver ++ " " ++ show code ++ " " ++ cmt

responseStatus : RawResponse String -> Maybe ResponseStatus
responseStatus (MkRawResponse r) with (lines r)
  | (x :: _) with (words x)
                | (_ :: _ :: []) = Nothing
                | (ver :: code :: cmt) with (the Int (cast code))
                  | 0 = Nothing
                  | n = Just $
                        MkResponseStatus ver n (unwords cmt)
                | _ = Nothing
  | [] = Nothing

partial   -- TODO: Nuke this annotation after https://github.com/relrod/idris-split/issues/1
splitDoubleCRLF : String -> (String, String)
splitDoubleCRLF s =
  case splitOn' "\r\n\r\n" s of
    (x :: [])      => (x, "")
    (x :: y :: []) => (x, y)
    _              => ("", "")

partial   -- TODO: Nuke this annotation after https://github.com/relrod/idris-split/issues/1
splitColon : String -> (String, String)
splitColon s =
  case splitOn' ":" s of
    (x :: [])      => (x, "")
    (x :: y :: []) => (x, y)
    _              => ("", "")

-- TODO: uwap please make this better
partial   -- TODO: Nuke this annotation after https://github.com/relrod/idris-split/issues/1
responseHeaders : RawResponse String -> List (String, String)
responseHeaders (MkRawResponse r) =
  let headersRaw = fst $ splitDoubleCRLF r
      headerLines = drop 1 . lines $ headersRaw
      headers = map splitColon headerLines
  in headers

partial   -- TODO: Nuke this annotation after https://github.com/relrod/idris-split/issues/1
responseBody : RawResponse String -> String
responseBody (MkRawResponse r) = snd . splitDoubleCRLF $ r<commit_msg>Fix case where multiple matches appear<commit_after>module Http.Response

import Data.Vect
import Http.RawResponse
import List.Split

%access public

record ResponseStatus where
  constructor MkResponseStatus
  responseStatusVersion : String
  responseStatusCode : Int
  responseStatusComment : String

instance Show ResponseStatus where
  show (MkResponseStatus ver code cmt) =
    "MkResponseStatus " ++ ver ++ " " ++ show code ++ " " ++ cmt

responseStatus : RawResponse String -> Maybe ResponseStatus
responseStatus (MkRawResponse r) with (lines r)
  | (x :: _) with (words x)
                | (_ :: _ :: []) = Nothing
                | (ver :: code :: cmt) with (the Int (cast code))
                  | 0 = Nothing
                  | n = Just $
                        MkResponseStatus ver n (unwords cmt)
                | _ = Nothing
  | [] = Nothing

partial   -- TODO: Nuke this annotation after https://github.com/relrod/idris-split/issues/1
splitDoubleCRLF : String -> (String, String)
splitDoubleCRLF s =
  case splitOn' "\r\n\r\n" s of
    (x :: [])      => (x, "")
    (x :: y :: []) => (x, y)
    (x :: y :: z)  => (x, y ++ concat (intersperse "\r\n" z))
    _              => ("", "")

partial   -- TODO: Nuke this annotation after https://github.com/relrod/idris-split/issues/1
splitColon : String -> (String, String)
splitColon s =
  case splitOn' ":" s of
    (x :: [])      => (x, "")
    (x :: y :: []) => (x, y)
    (x :: y :: z)  => (x, y ++ concat (intersperse ":" z))
    _              => ("", "")

-- TODO: uwap please make this better
partial   -- TODO: Nuke this annotation after https://github.com/relrod/idris-split/issues/1
responseHeaders : RawResponse String -> List (String, String)
responseHeaders (MkRawResponse r) =
  let headersRaw = fst $ splitDoubleCRLF r
      headerLines = drop 1 . lines $ headersRaw
      headers = map splitColon headerLines
  in headers

partial   -- TODO: Nuke this annotation after https://github.com/relrod/idris-split/issues/1
responseBody : RawResponse String -> String
responseBody (MkRawResponse r) = snd . splitDoubleCRLF $ r
{'fraction_non_alphanumeric': 0.13098174642328564, 'fraction_numerical': 0.0029600394671928957, 'mean_word_length': 2.6012433392539966, 'pattern_counts': {'":': 3, '<': 3, '<?xml version=': 0, '>': 27, 'https://': 8, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '474322', 'n_tokens_mistral': 1245, 'n_tokens_neox': 1214, 'n_words': 365}