<commit_before>module Server where
import Control.Applicative
import Control.Exception
import Control.Monad
import Data.Char
import qualified Network
import Network.Socket
import Text.Hexdump


main =
    bracket
        (Network.listenOn (Network.PortNumber 2593))
        sClose
        (forever . accept')
		
accept' service = bracket (fst <$> accept service) sClose handlePeer

handlePeer :: Socket -> IO ()
handlePeer peer = do
  (_,4) <- recvLen peer 4 -- throw away first 4 bytes its junk
  (dta,62) <- recvLen peer 62
  logHex "IB: localhost" dta
  logHex "OB: localhost" denied
  2 <- send peer denied
  return ()

fmtHex :: String -> String -> String
fmtHex title dta =
  title ++ "\n" ++
  "-----------------------------------------------------------------------------\n" ++
  (hexdump 0 dta) ++ "\n"

logHex :: String -> String -> IO ()
logHex title dta = putStrLn (fmtHex title dta)

denied = [chr 0x82, chr 0x4]
<commit_msg>converted to bytestring for sending/receiving<commit_after>module Server where
import Control.Applicative ((<$>))
import Control.Exception
import Control.Monad
import qualified Data.ByteString.Char8 as B
import Data.Char
import Network (PortID(..), listenOn)
import Network.Socket (Socket, accept, sClose)
import Network.Socket.ByteString
import Text.Hexdump


main =
    bracket
        (listenOn (PortNumber 2593))
        sClose
        (forever . accept')
		
accept' service = bracket (fst <$> accept service) sClose handlePeer

handlePeer :: Socket -> IO ()
handlePeer peer = do
  recvExactly peer 4 -- throw away first 4 bytes its junk
  dta <- recvExactly peer 62
  logHex "IB: localhost" dta
  logHex "OB: localhost" denied
  2 <- send peer denied
  return ()

fmtHex :: String -> B.ByteString -> String
fmtHex title dta =
  title ++ "\n" ++
  "-----------------------------------------------------------------------------\n" ++
  hexdump 0 (B.unpack dta) ++ "\n"

logHex :: String -> B.ByteString -> IO ()
logHex title dta = putStrLn (fmtHex title dta)

denied = B.pack [chr 0x82, chr 0x4]

recvExactly :: Socket -> Int -> IO B.ByteString
recvExactly peer nbytes
    | nbytes <= 0 = return B.empty
	| otherwise = do res <- recv peer nbytes
	                 if res == B.empty
		               then error "peer closed"
			           else B.append res <$> recvExactly peer (nbytes - B.length res)

{'fraction_non_alphanumeric': 0.1682322801024765, 'fraction_numerical': 0.014944491887275833, 'mean_word_length': 2.3858381502890174, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 19, 'https://': 0, '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': '6762459', 'n_tokens_mistral': 747, 'n_tokens_neox': 687, 'n_words': 245}