Skip to content

Commit

Permalink
Merge pull request #105 from edsko/edsko/string-as-authority
Browse files Browse the repository at this point in the history
Use `String` for `Authority`
  • Loading branch information
kazu-yamamoto authored Jan 15, 2024
2 parents 97b7338 + fdb1f7b commit 6b2fc4b
Show file tree
Hide file tree
Showing 8 changed files with 18 additions and 17 deletions.
7 changes: 5 additions & 2 deletions Network/HTTP2/Client/Run.hs
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@ import Network.Socket (SockAddr)
import UnliftIO.Async
import UnliftIO.Concurrent
import UnliftIO.STM
import qualified Data.ByteString.UTF8 as UTF8

import Imports
import Network.HTTP2.Client.Types
import Network.HTTP2.Frame
import Network.HTTP2.H2
import Network.HTTP.Types (Header)

-- | Client configuration
data ClientConfig = ClientConfig
Expand Down Expand Up @@ -172,11 +174,12 @@ sendRequest ctx@Context{..} mgr scheme auth (Request req) = do
-- Otherwise, it would be re-enqueue because of empty
-- resulting in out-of-order.
-- To implement this, 'tbqNonEmpty' is used.
let hdr1
let hdr1, hdr2 :: [Header]
hdr1
| scheme /= "" = (":scheme", scheme) : hdr0
| otherwise = hdr0
hdr2
| auth /= "" = (":authority", auth) : hdr1
| auth /= "" = (":authority", UTF8.fromString auth) : hdr1
| otherwise = hdr1
req' = req{outObjHeaders = hdr2}
-- FLOW CONTROL: SETTINGS_MAX_CONCURRENT_STREAMS: send: respecting peer's limit
Expand Down
4 changes: 2 additions & 2 deletions Network/HTTP2/H2/Context.hs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ data ServerInfo = ServerInfo

data ClientInfo = ClientInfo
{ scheme :: ByteString
, authority :: ByteString
, authority :: Authority
}

toServerInfo :: RoleInfo -> ServerInfo
Expand All @@ -46,7 +46,7 @@ toClientInfo _ = error "toClientInfo"
newServerInfo :: IO RoleInfo
newServerInfo = RIS . ServerInfo <$> newTQueueIO

newClientInfo :: ByteString -> ByteString -> RoleInfo
newClientInfo :: ByteString -> Authority -> RoleInfo
newClientInfo scm auth = RIC $ ClientInfo scm auth

----------------------------------------------------------------
Expand Down
3 changes: 2 additions & 1 deletion Network/HTTP2/H2/Receiver.hs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import Network.Control
import UnliftIO.Concurrent
import qualified UnliftIO.Exception as E
import UnliftIO.STM
import qualified Data.ByteString.UTF8 as UTF8

import Imports hiding (delete, insert)
import Network.HPACK
Expand Down Expand Up @@ -365,7 +366,7 @@ push header@FrameHeader{streamId} bs ctx = do
(_, vt) <- hpackDecodeHeader frag streamId ctx
let ClientInfo{..} = toClientInfo $ roleInfo ctx
when
( getHeaderValue tokenAuthority vt == Just authority
( getHeaderValue tokenAuthority vt == Just (UTF8.fromString authority)
&& getHeaderValue tokenScheme vt == Just scheme
)
$ do
Expand Down
2 changes: 1 addition & 1 deletion Network/HTTP2/H2/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import Network.HTTP2.H2.File
type Scheme = ByteString

-- | Authority.
type Authority = ByteString
type Authority = String

-- | Path.
type Path = ByteString
Expand Down
3 changes: 2 additions & 1 deletion Network/HTTP2/Server.hs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ module Network.HTTP2.Server (
import Data.ByteString.Builder (Builder)
import Data.IORef (readIORef)
import qualified Network.HTTP.Types as H
import qualified Data.ByteString.UTF8 as UTF8

import Imports
import Network.HPACK
Expand Down Expand Up @@ -147,7 +148,7 @@ requestPath (Request req) = getHeaderValue tokenPath vt

-- | Getting the authority from a request.
requestAuthority :: Request -> Maybe Authority
requestAuthority (Request req) = getHeaderValue tokenAuthority vt
requestAuthority (Request req) = UTF8.toString <$> getHeaderValue tokenAuthority vt
where
(_, vt) = inpObjHeaders req

Expand Down
3 changes: 2 additions & 1 deletion http2.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,8 @@ library
network-control >= 0.0.2 && < 0.1,
unix-time >= 0.4.11 && < 0.5,
time-manager >= 0.0.1 && < 0.1,
unliftio >= 0.2 && < 0.3
unliftio >= 0.2 && < 0.3,
utf8-string >= 1.0 && < 1.1

executable client
main-is: client.hs
Expand Down
9 changes: 2 additions & 7 deletions test/HTTP2/ClientSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ module HTTP2.ClientSpec where
import Control.Concurrent
import qualified Control.Exception as E
import Control.Monad
import Data.ByteString (ByteString)
import Data.ByteString.Builder (byteString)
import qualified Data.ByteString.Char8 as C8
import Data.Foldable (for_)
import Data.Maybe
import Data.Traversable (for)
Expand All @@ -32,16 +30,13 @@ port = show $ unsafePerformIO (randomPort <$> getStdGen)
host :: String
host = "127.0.0.1"

host' :: ByteString
host' = C8.pack host

spec :: Spec
spec = do
describe "client" $ do
it "receives an error if scheme is missing" $
E.bracket (forkIO $ runServer defaultServer) killThread $ \_ -> do
threadDelay 10000
runClient "" host' (defaultClient []) `shouldThrow` connectionError
runClient "" host (defaultClient []) `shouldThrow` connectionError

it "receives an error if authority is missing" $
E.bracket (forkIO $ runServer defaultServer) killThread $ \_ -> do
Expand All @@ -51,7 +46,7 @@ spec = do
it "receives an error if authority and host are different" $
E.bracket (forkIO $ runServer defaultServer) killThread $ \_ -> do
threadDelay 10000
runClient "http" host' (defaultClient [("Host", "foo")])
runClient "http" host (defaultClient [("Host", "foo")])
`shouldThrow` connectionError

it "does not deadlock (in concurrent setting)" $
Expand Down
4 changes: 2 additions & 2 deletions test/HTTP2/ServerSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ runClient :: (Socket -> BufferSize -> IO Config) -> IO ()
runClient allocConfig =
runTCPClient host port $ runHTTP2Client
where
auth = C8.pack host
auth = host
cliconf = C.defaultClientConfig{C.authority = auth}
runHTTP2Client s =
E.bracket
Expand Down Expand Up @@ -315,7 +315,7 @@ runAttack :: (C.ClientIO -> IO ()) -> IO ()
runAttack attack =
runTCPClient host port $ runHTTP2Client
where
auth = C8.pack host
auth = host
cliconf = C.defaultClientConfig{C.authority = auth}
runHTTP2Client s =
E.bracket
Expand Down

0 comments on commit 6b2fc4b

Please sign in to comment.