~garritfra/taurus

taurus: Infer gemini:// scheme in the URL v3 NEEDS REVISION

Alexey Yerin: 1
 Infer gemini:// scheme in the URL

 2 files changed, 27 insertions(+), 17 deletions(-)
#340516 ci.yml success
> Could you turn this into a `match` statement? I think that would be
> easier to read and less error-prone.
No, it's not a match on the whole string
> It's a bit tricky. If I'm not mistaken, "http://example.com" would end
> up as "gemini://http://example.com", so we would need to handle some
> more cases there.
For now it throws 51 in that case, to properly solve this I think that
we should find first `://` (if it exists) and check if part of URL to
this point is `gemini`. And if not, return 53 (proxy request refused)
because protocol is invalid.
Export patchset (mbox)
How do I use this?

Copy & paste the following snippet into your terminal to import this patchset into git:

curl -s https://lists.sr.ht/~garritfra/taurus/patches/14954/mbox | git am -3
Learn more about email & git
View this thread in the archives

[PATCH taurus v3] Infer gemini:// scheme in the URL Export this patch

Signed-off-by: Alexey Yerin <yerinalexey98fd@gmail.com>
---
Removed unnecessary code

 src/gemini.rs | 39 ++++++++++++++++++++++++++-------------
 src/main.rs   |  5 +----
 2 files changed, 27 insertions(+), 17 deletions(-)

diff --git a/src/gemini.rs b/src/gemini.rs
index 9963012..e0853f8 100644
--- a/src/gemini.rs
+++ b/src/gemini.rs
@@ -27,6 +27,16 @@ impl FromStr for GeminiRequest {
    type Err = TaurusError;

    fn from_str(s: &str) -> TaurusResult<Self> {
        let mut s = s.to_string();

        // Implicitly add gemini:// scheme
        if s.starts_with("//") {
            s = format!("gemini:{}", s);
        }
        if !s.starts_with("gemini://") {
            s = format!("gemini://{}", s);
        }

        // Extract and parse the url from the request.
        let raw = s
            .trim_end_matches(0x0 as char)
@@ -88,32 +98,35 @@ impl GeminiResponse {
mod tests {
    use super::*;

    #[test]
    fn parse_request() {
        let raw = "gemini://example.space\r\n";

    fn check_request(raw: &str, expected_url: &str) {
        let req = GeminiRequest::parse(raw).unwrap();

        assert_eq!(
            req,
            GeminiRequest {
                url: Url::parse("gemini://example.space").unwrap()
                url: Url::parse(expected_url).unwrap()
            }
        );
    }

    #[test]
    fn parse_malformed_request() {
        let raw = "gemini://example.space";
    fn parse_request() {
        check_request("gemini://example.space\r\n", "gemini://example.space");
    }

        match GeminiRequest::parse(raw) {
            Err(TaurusError::InvalidRequest(_)) => {}
            x => panic!("expected TaurusError::InvalidRequest, got: {:?}", x),
        }
    #[test]
    fn parse_without_scheme() {
        check_request("example.space\r\n", "gemini://example.space");
    }

    #[test]
    fn parse_invalid_request_url() {
        let raw = "foobar@example.com\r\n";
    fn parse_without_scheme_double_slash() {
        check_request("//example.space\r\n", "gemini://example.space");
    }

    #[test]
    fn parse_malformed_request() {
        let raw = "gemini://example.space";

        match GeminiRequest::parse(raw) {
            Err(TaurusError::InvalidRequest(_)) => {}
diff --git a/src/main.rs b/src/main.rs
index 86d899b..90de12e 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -134,10 +134,7 @@ fn handle_client(mut stream: TlsStream<TcpStream>, static_root: &str) -> TaurusR
        .read(&mut buffer)
        .map_err(TaurusError::StreamReadFailed)?;

    let mut raw_request = String::from_utf8_lossy(&buffer[..]).into_owned();
    if !raw_request.starts_with("gemini://") {
        raw_request = format!("gemini://{}", raw_request);
    }
    let raw_request = String::from_utf8_lossy(&buffer[..]).into_owned();

    let request = GeminiRequest::parse(&raw_request)?;
    let url_path = request.file_path();
-- 
2.29.2
builds.sr.ht
taurus/patches/ci.yml: SUCCESS in 1m57s

[Infer gemini:// scheme in the URL][0] v3 from [Alexey Yerin][1]

[0]: https://lists.sr.ht/~garritfra/taurus/patches/14954
[1]: mailto:yerinalexey98fd@gmail.com

✓ #340516 SUCCESS taurus/patches/ci.yml https://builds.sr.ht/~garritfra/job/340516