8
\$\begingroup\$

So, normally, this is a forbidden loophole to fetch the desired output from an external source. But this challenge is an exception ! In this challenge you will have to fetch data from the internet and output it to the SDTOUT.

The task

  1. For the purpose of this challenge, you'll be the owner of the imaginary a.pi domain!
  2. You will need to take the first argument from STDIN, pass it to a.pi/{arg}, and print the result to STDOUT.
  3. You don't need to handle errors, the server is always up and running !
  4. This is a , the winner is the shortest (in bytes or equivalent) valid answer.

Rules

  1. You are allowed to invoke curl and wget [1] commands as child/sub-processes (like child_process with NodeJS).
  2. The ressource at a.pi will only be available in the http and https protocol in their default ports (80 and 443 respectively), you don't need to include the http:// if your language allow to ommit it.
  3. Only the desired output (http response) should be printed, nothing else (no GET http://a.pi/: or whatever).
  4. The input might contain spaces or otherwise reserved characters, and need to be percent-encoded.
    Spaces ( ) should be replaced with %20 and % with %25 (As spaces cannot be in URLs). The other characters (including reserved ones) don't need to be encoded, but you can encode them (including unreserved characters) if that makes your code shorter.
  5. If your language/environement doesn't have a STDIN/STDOUT (like in a browser), the shortest equivalent can be used (like prompt() and alert() in javascript).
  6. The CORS Headers are open for any external requests (Access-Control-Allow-Origin: *), if you where wondering.

Note: to test your program, you can use any domain and then replace it with a.pi for the submission.

[1] While it should works the same everywhere, I'd recommend you to precise which OS you used for testing.

Updates

Rule 4 have been modified as it was the hardest part is the percent-encoding, which is already a submitted code golf, and the exact rules of the encoding where unclear.

Note: this the first code-golf challenge that I submit, and I didn't knew about the sandbox before. In the future, I'll definitely post here before submitting.
Also, this change doesn't affect previous entries (Except for their length).

\$\endgroup\$
6
  • 3
    \$\begingroup\$ Points 1 and 2 are unclear and basically unobservable. curl and wgets are essentially language-specific tools, and what do you mean by "the included library"? I suggest you use the Sandbox to get feedback on challenge ideas before posting them to main \$\endgroup\$ Commented Nov 23, 2021 at 13:23
  • 1
    \$\begingroup\$ @cairdcoinheringaahing Thanks for pointing this out! I removed the rule 1 (and shifted the rule numbers) So now you are allowed to use these command-line tools no matter what, as choosing which methot is shorter (like child_process vs http.get()) is part of the challenge. Btw I didn't knew about the Sandbox, I'll use it for next golf challenges that comes to my mind. \$\endgroup\$
    – CreaZyp154
    Commented Nov 23, 2021 at 13:59
  • 1
    \$\begingroup\$ Are spaces the only characters we should percent-encode (%20)? If not, what are all possible inputs (Letters/digits/spaces only? Printable ASCII only?), and which need to be encoded besides spaces? Also, are + allowed to encode spaces specifically? \$\endgroup\$ Commented Nov 23, 2021 at 15:11
  • 1
    \$\begingroup\$ Does this answer your question? Percent-Encode a String \$\endgroup\$
    – pajonk
    Commented Nov 23, 2021 at 17:13
  • 1
    \$\begingroup\$ This looks like the main task here is percent-encoding the input string (for which we have a challenge: codegolf.stackexchange.com/questions/89767/…). \$\endgroup\$
    – pajonk
    Commented Nov 23, 2021 at 17:14

7 Answers 7

2
\$\begingroup\$

05AB1E, 29 20 bytes

„ %DÇh'%ì‡"a.pi/"ì.w

-9 bytes now that the encoding rule (4) has changed.

Try it online (where .w will act as no-op, since it's disabled on TIO).

Original 29 bytes answer encoding all reserved and non-ASCII characters:

žj“"+-.<>“«мDÇh'%ì‡"a.pi/"ì.w

\$\frac{2}{3}rd\$ of the program is used to encode the characters (19 bytes to be exact).

Try it online (where .w will act as no-op, since it's disabled on TIO).

Explanation:

„ %                # Push string " %"
   D               # Duplicate this string
    Ç              # Convert it to a list of codepoint integers
     h             # Convert each integer to hexadecimal
      '%ì         '# Prepend a "%" in front of each
         ‡         # Transliterate all characters in the (implicit) input-string
          "a.pi/"ì # Prepend "a.pi/" in front of it
.w                 # Browse this page (with leading "http://" implicitly added) and
                   # read and push its contents
                   # (after which this is output implicitly as result)

žj                 # Push builtin "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_"
  “"+-.<>“«       "# Append string '"+-.<>'
           м       # Remove all these characters from the (implicit) input-string,
                   # so we're left with all reserved and/or non-ASCII characters
DÇh'%ì‡"a.pi/"ì.w '# Same as above
\$\endgroup\$
2
  • 3
    \$\begingroup\$ I half expected the "bytes" link in "19 bytes" to link to The Nineteenth Byte :P \$\endgroup\$ Commented Nov 23, 2021 at 18:17
  • \$\begingroup\$ Shouldn't that be "⅔ʳᵈˢ"? \$\endgroup\$
    – Neil
    Commented Nov 24, 2021 at 16:52
2
\$\begingroup\$

Javascript (ES6), 26 23 bytes

x=>fetch('/'+escape(x))

Percent encode builtin FTW!!!

Returns a Promise containing an object with a text method, which when called with no properties returns the result, allowed by this.

Must be run on the a.pi domain, allowed by this consensus. Since this is impossible, try running the following in your console, on this site:

f=
x=>fetch('/'+escape(x))

f('questions').then(x=>x.text()).then(console.log)

(This requests the questions page)

\$\endgroup\$
2
  • \$\begingroup\$ Can you use escape instead of encodeURI? \$\endgroup\$ Commented Nov 24, 2021 at 11:36
  • \$\begingroup\$ @DomHastings Yep! \$\endgroup\$
    – emanresu A
    Commented Nov 24, 2021 at 18:27
1
\$\begingroup\$

Perl 5 -nlMv5.10, 50 bytes

s/\W/sprintf"%%%02x",ord$&/ge;say`curl -s a.pi/$_`

Can't try it online unless you swap the two ` chars with " to output the curl command with it's arguments instead of it's response. Percent-encodes all chars except a-z, A-Z, 0-9 and _.

\$\endgroup\$
1
\$\begingroup\$

PHP, 54 47 bytes

<?=join(file('http://a.pi/'.urlencode($argn)));

Try it online!

Note: TIO does not allow external requests, though it probably doesn't really matter since a.pi does not really exist.

-7 bytes thx to @Kaddath!

\$\endgroup\$
3
  • 2
    \$\begingroup\$ You should be able to replace file_get_contents() by join(file()) and save 7 bytes ;) \$\endgroup\$
    – Kaddath
    Commented Nov 25, 2021 at 8:29
  • \$\begingroup\$ @Kaddath nice one! Thx buddy! \$\endgroup\$
    – 640KB
    Commented Nov 25, 2021 at 16:43
  • \$\begingroup\$ Using readfile() would make it even shorter by a pair of parenthesis and the equal sign. \$\endgroup\$
    – manatwork
    Commented Nov 28, 2021 at 21:52
0
\$\begingroup\$

C# (Visual C# Interactive Compiler), 77 bytes

-10 bytes thanks to Kevin Cruijssen

a=>new System.Net.WebClient().DownloadString("a.pi/"+Uri.EscapeDataString(a))

Try it online!

C# (Visual C# Interactive Compiler), 34 bytes

If you only need to return the URL + encoded query string like in some of the answers.

a=>"a.pi/"+Uri.EscapeDataString(a)

Try it online!

\$\endgroup\$
2
  • 1
    \$\begingroup\$ You can remove the {return ;}: 77 bytes \$\endgroup\$ Commented Nov 24, 2021 at 18:28
  • \$\begingroup\$ @KevinCruijssen, thanks! \$\endgroup\$
    – jdt
    Commented Nov 24, 2021 at 19:35
0
\$\begingroup\$

Factor + http.client, 42 bytes

[ url-encode "a.pi/"prepend http-get nip ]

Try it online!

\$\endgroup\$
0
\$\begingroup\$

GNU AWK, 122 bytes

BEGIN{RS=ORS="\r\n";s="/inet/tcp/0/a.pi/80"}1gsub("%","%25")gsub(FS,"%20"){print"GET /"$0|&s;while((s|&getline)>0)print$0}

Try it online!

NO WAY! I didn't know AWK could do that!

BEGIN{RS=ORS="\r\n";

We start by setting \r\n(CR LF) as the trailing character of the print function, as the default is only \n.

s="/inet/tcp/0/a.pi/80"}

And then we set the server we want to communicate with.

1gsub("%","%25")gsub(FS,"%20")

This AWK pattern will always return true, even if there are no spaces or % in the input.

{print"GET /"$0|&s;while((s|&getline)>0)print$0}

The last piece of code GETs the domain response until the last line.

For this code to work, make sure to input the wanted ARG as in a.pi/{ARG}, hit Enter, and then Ctrl+D (or your most favorite ASCII-4-End-of-transmission way of doing it).

\$\endgroup\$
1
  • \$\begingroup\$ Point 3. says “You don't need to handle errors”, so you can skip comparing getline against 0. Also print without argument will print $0, so no need to specify it. \$\endgroup\$
    – manatwork
    Commented Nov 28, 2021 at 22:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.