Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/Simplex/FileTransfer/Description.hs
Original file line number Diff line number Diff line change
Expand Up @@ -280,13 +280,13 @@ fileDescriptionURI :: ValidFileDescription 'FRecipient -> FileDescriptionURI
fileDescriptionURI vfd = FileDescriptionURI SSSimplex vfd mempty

instance StrEncoding FileDescriptionURI where
strEncode FileDescriptionURI {scheme, description, clientData} = mconcat [strEncode scheme, "/file", "#/?", queryStr]
strEncode FileDescriptionURI {scheme, description, clientData} = mconcat [strEncode scheme, "file", "#/?", queryStr]
where
queryStr = strEncode $ QSP QEscape qs
qs = ("desc", strEncode description) : maybe [] (\cd -> [("data", encodeUtf8 cd)]) clientData
strP = do
scheme <- strP
_ <- "/file" <* optional (A.char '/') <* "#/?"
_ <- "file" <* optional (A.char '/') <* "#/?"
query <- strP
description <- queryParam "desc" query
let clientData = safeDecodeUtf8 <$> queryParamStr "data" query
Expand Down
23 changes: 12 additions & 11 deletions src/Simplex/Messaging/Agent/Protocol.hs
Original file line number Diff line number Diff line change
Expand Up @@ -1234,7 +1234,7 @@ instance ConnectionModeI m => StrEncoding (ConnectionRequestUri m) where
where
crEncode :: ByteString -> ConnReqUriData -> (Maybe (RcvE2ERatchetParamsUri 'C.X448), Maybe RatchetKeyId) -> ByteString
crEncode crMode ConnReqUriData {crScheme, crAgentVRange, crSmpQueues, crClientData} (e2eParams, rk) =
strEncode crScheme <> "/" <> crMode <> "#/?" <> queryStr
strEncode crScheme <> crMode <> "#/?" <> queryStr
where
queryStr =
strEncode . QSP QEscape $
Expand Down Expand Up @@ -1289,7 +1289,7 @@ instance StrEncoding AConnectionRequestUri where
connReqUriP :: Maybe ServiceScheme -> Parser AConnectionRequestUri
connReqUriP overrideScheme = do
crScheme <- (`fromMaybe` overrideScheme) <$> strP -- always parse, but use the passed one if any
crMode <- A.char '/' *> crModeP <* optional (A.char '/') <* "#/?"
crMode <- crModeP <* optional (A.char '/') <* "#/?"
query <- strP
aVRange <- queryParam "v" query
crSmpQueues <- queryParamParser queuesP "smp" query
Expand Down Expand Up @@ -1737,11 +1737,11 @@ instance ConnectionModeI m => StrEncoding (ConnShortLink m) where
CSLContact sch ct srv (LinkKey k) -> slEncode sch srv (toLower $ ctTypeChar ct) "" k
where
slEncode sch (SMPServer (h :| hs) port (C.KeyHash kh)) linkType lnkId k =
B.concat [authority, "/", B.singleton linkType, "#", lnkIdStr, B64.encodeUnpadded k, queryStr]
B.concat [strEncode baseUri, B.singleton linkType, "#", lnkIdStr, B64.encodeUnpadded k, queryStr]
where
(authority, paramHosts) = case sch of
SLSSimplex -> ("simplex:", h : hs)
SLSServer -> ("https://" <> strEncode h, hs)
(baseUri, paramHosts) = case sch of
SLSSimplex -> (SSSimplex, h : hs)
SLSServer -> (SSAppServer (SrvLoc h ""), hs)
lnkIdStr = if B.null lnkId then "" else B64.encodeUnpadded lnkId <> "/"
queryStr = if B.null query then "" else "?" <> query
query =
Expand All @@ -1756,7 +1756,7 @@ instance StrEncoding AConnShortLink where
strEncode (ACSL _ l) = strEncode l
{-# INLINE strEncode #-}
strP = do
(sch, h_) <- authorityP <* A.char '/'
(sch, h_) <- baseUriP
ct_ <- contactTypeP <* optional (A.char '/') <* A.char '#'
case ct_ of
Nothing -> do
Expand All @@ -1769,10 +1769,11 @@ instance StrEncoding AConnShortLink where
srv <- serverQueryP h_
pure $ ACSL SCMContact $ CSLContact sch ct srv (LinkKey k)
where
authorityP =
"simplex:" $> (SLSSimplex, Nothing)
<|> "https://" *> ((SLSServer,) . Just <$> strP)
<|> fail "bad short link scheme"
baseUriP =
strP >>= \case
SSSimplex -> pure (SLSSimplex, Nothing)
SSAppServer (SrvLoc h "") -> pure (SLSServer, Just h)
SSAppServer _ -> fail "bad short link scheme"
contactTypeP = do
Just <$> (A.anyChar >>= ctTypeP . toUpper)
<|> A.char 'i' $> Nothing
Expand Down
10 changes: 5 additions & 5 deletions src/Simplex/Messaging/ServiceScheme.hs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module Simplex.Messaging.ServiceScheme
simplexChat,
) where

import Control.Applicative ((<|>))
import Control.Applicative (optional, (<|>))
import qualified Data.Attoparsec.ByteString.Char8 as A
import qualified Data.ByteString.Char8 as B
import Data.Functor (($>))
Expand All @@ -20,11 +20,11 @@ data ServiceScheme = SSSimplex | SSAppServer SrvLoc

instance StrEncoding ServiceScheme where
strEncode = \case
SSSimplex -> "simplex:"
SSAppServer srv -> "https://" <> strEncode srv
SSSimplex -> "simplex:/"
SSAppServer srv -> "https://" <> strEncode srv <> "/"
strP =
"simplex:" $> SSSimplex
<|> "https://" *> (SSAppServer <$> strP)
"simplex:" *> optional (A.char '/') $> SSSimplex
<|> "https://" *> (SSAppServer <$> strP) <* A.char '/'

data SrvLoc = SrvLoc TransportHost ServiceName
deriving (Eq, Ord, Show)
Expand Down
7 changes: 6 additions & 1 deletion src/Simplex/Messaging/SimplexName.hs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import Simplex.Messaging.Agent.Store.DB (Binary (..), FromField (..), ToField (.
import Simplex.Messaging.Encoding (Encoding (..))
import Simplex.Messaging.Encoding.String
import Simplex.Messaging.Parsers (defaultJSON, dropPrefix, enumJSON)
import Simplex.Messaging.ServiceScheme (ServiceScheme (..))
import Simplex.Messaging.Util (eitherToMaybe, safeDecodeUtf8, (<$?>))

data SimplexNameInfo = SimplexNameInfo
Expand Down Expand Up @@ -110,8 +111,12 @@ boundedNonSpace = do
instance StrEncoding SimplexNameInfo where
strEncode SimplexNameInfo {nameType, nameDomain} =
strEncode nameType <> strEncode nameDomain
strP = optional "simplex:/name" *> ((strP >>= infoP) <|> infoP NTPublicGroup)
strP = optional nameSchemeP *> (strP >>= infoP)
where
nameSchemeP =
strP >>= \case
SSSimplex -> "name"
SSAppServer _ -> fail "bad name scheme"
infoP NTPublicGroup = SimplexNameInfo NTPublicGroup <$> (strP <|> bareName)
infoP NTContact = SimplexNameInfo NTContact <$> strP
bareName = parseBare . safeDecodeUtf8 <$?> boundedNonSpace
Expand Down
11 changes: 11 additions & 0 deletions tests/AgentTests/ConnectionRequestTests.hs
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,17 @@ connectionRequestTests =
CSLContact SLSSimplex CCTContact shortSrv (LinkKey "0123456789abcdef0123456789abcdef") #==# "simplex:/a#MDEyMzQ1Njc4OWFiY2RlZjAxMjM0NTY3ODlhYmNkZWY?h=smp.simplex.im"
CSLInvitation SLSSimplex srv (EntityId "0123456789abcdef01234567") (LinkKey "0123456789abcdef0123456789abcdef") #==# "simplex:/i#MDEyMzQ1Njc4OWFiY2RlZjAxMjM0NTY3/MDEyMzQ1Njc4OWFiY2RlZjAxMjM0NTY3ODlhYmNkZWY?h=smp.simplex.im%2Cjjbyvoemxysm7qxap7m5d5m35jzv5qq6gnlv7s4rsn7tdwwmuqciwpid.onion&p=5223&c=1234-w"
CSLInvitation SLSSimplex shortSrv (EntityId "0123456789abcdef01234567") (LinkKey "0123456789abcdef0123456789abcdef") #==# "simplex:/i#MDEyMzQ1Njc4OWFiY2RlZjAxMjM0NTY3/MDEyMzQ1Njc4OWFiY2RlZjAxMjM0NTY3ODlhYmNkZWY?h=smp.simplex.im"
it "should parse simplex: links without the slash after the scheme" $ do
let channel = CSLContact SLSSimplex CCTChannel shortSrv (LinkKey "0123456789abcdef0123456789abcdef")
name = SimplexNameInfo NTPublicGroup (SimplexDomain TLDSimplex "alice" [])
contactAddress #== ("simplex:contact#/?v=6-8&smp=" <> url queueStr)
connectionRequest #== ("simplex:invitation#/?v=6-8&smp=" <> url queueStr <> "&e2e=" <> testE2ERatchetParamsStrUri)
channel #==# "simplex:/c#MDEyMzQ1Njc4OWFiY2RlZjAxMjM0NTY3ODlhYmNkZWY?h=smp.simplex.im"
channel #== "simplex:c#MDEyMzQ1Njc4OWFiY2RlZjAxMjM0NTY3ODlhYmNkZWY?h=smp.simplex.im"
name #== "simplex:/name#alice.simplex"
name #== "simplex:name#alice.simplex"
(strDecode "https://simplex.chat/name#alice.simplex" :: Either String SimplexNameInfo) `shouldSatisfy` isLeft
(strDecode "simplex:namealice" :: Either String SimplexNameInfo) `shouldSatisfy` isLeft
it "should shorten / restore short links" $ do
let contact = CSLContact SLSServer CCTContact
shortenShortLink [srv] (contact srv (LinkKey "0123456789abcdef0123456789abcdef"))
Expand Down
6 changes: 3 additions & 3 deletions tests/CoreTests/EncodingTests.hs
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,12 @@ encodingTests = modifyMaxSuccess (const 1000) $ do
shouldReject @TransportHost "[smp.simplex.im]"
describe "Encoding service locations" $ do
it "should parse bracketed IPv6 host with port" $ do
strDecode @ServiceScheme "https://[2001:db8::1]:8443"
strDecode @ServiceScheme "https://[2001:db8::1]:8443/"
`shouldBe` Right (SSAppServer $ SrvLoc "2001:db8::1" "8443")
strEncode (SSAppServer $ SrvLoc "2001:db8::1" "8443")
`shouldBe` "https://[2001:db8::1]:8443"
`shouldBe` "https://[2001:db8::1]:8443/"
it "should reject bracketed non-IPv6 host" $
shouldReject @ServiceScheme "https://[simplex.chat]:8443"
shouldReject @ServiceScheme "https://[simplex.chat]:8443/"
describe "Encoding protocol servers" $ do
it "should parse bracketed IPv6 server host with port" $
case strDecode @XFTPServer "xftp://1234-w==@[2001:db8::1]:443" of
Expand Down
9 changes: 9 additions & 0 deletions tests/FileDescriptionTests.hs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ fileDescriptionTests = do
describe "file description URIs" $ do
it "round trip file description URI" testFileDescriptionURI
it "round trip file description URI with extra JSON" testFileDescriptionURIExtras
it "parse file description URI without the slash after the scheme" testFileDescriptionURIWithoutSlash

fileDescPath :: FilePath
fileDescPath = "tests/fixtures/file_description.yaml"
Expand Down Expand Up @@ -250,6 +251,14 @@ testFileDescriptionURIExtras = do
let descr = FileDescriptionURI SSSimplex vfd $ Just "{\"something\":\"extra\",\"more\":true}"
strDecode (strEncode descr) `shouldBe` Right descr

testFileDescriptionURIWithoutSlash :: IO ()
testFileDescriptionURIWithoutSlash = do
vfd <- either fail pure $ validateFileDescription fileDesc
let descr = FileDescriptionURI SSSimplex vfd mempty
(prefix, rest) = B.splitAt 9 $ strEncode descr
prefix `shouldBe` "simplex:/"
strDecode ("simplex:" <> rest) `shouldBe` Right descr

testHashChunks :: HasTestEnv => IO ()
testHashChunks = do
let sizes = [0, 65535, 65536, 100000, 200000]
Expand Down
Loading