Add simple echo CGI script.
[newgopher.git] / public_gopher / newgopher.txt
blob7548797b72fbff5ad65905a5922f17b1a09d4bcf
1 # New Gopher
3 ## Prerequisites
5 This  proposal  will  use  many terms as used in the original Gopher RFC
6 1436.
8 ## Encoding
10 All  strings  sent  have  to be in UTF‐8, except for »binary« or »data«.
11 These are binary data streams and should be handled like that. No Byte-
12 Order-Mark (BOM) must be sent.
14 ### URI
16 URIs  do not allow full UTF‐8 encoding. If for example punycode is used,
17 then the client has to convert them into UTF‐8, so  new  gopher  servers
18 will understand it. UTF-8 should be enforced there.
20 Because  of  the  different type behaviour do the URIs operate without a
21 item type. See the section on how to determine the file type  below  for
22 further details.
24 ## Request
26 Requests  can be sent over every transport protocol, which can guarantee
27 the data stream to be correct. No error detection is done in this proto‐
28 col.  The main intention for this protocol is it to be used over TCP/IP,
29 using mandatory TLS encryption. TLS should be used over other  transport
30 protocols too.
32 ### Simple
34         C->S: selector\r\n
35         S->C: $size\t$mime-type\r\n
36               [data]
38 The  $size  line  is defining the number of bytes that will follow after
39 the »$size\t$mime‐type\r\n« line. If $size  is  »‐1«,  then  the  client
40 should listen until the connection is closed.
42 The $mime-type is defining what data will follow.
44 ### Complex
46         C->S: selector\tsearch\trange\r\n
47               $size\t$mime-type\r\n
48               [data]
49         S->C: $size\t$mime-type\r\n
50               [menu/binary/metadata]
52 In a complex request the »\t« in the first retrieve line is seen as flag
53 to retrieve a further line that will tell the server how much  data  the
54 client   will send and what kind of data this will be ($mime‐type).  The
55 server may apply rules or selector‐specific rules how much data  is  al‐
56 lowed to be retrieved by a client.
58 The  $size  line  is defining the number of bytes that will follow after
59 the »$size\t$mime‐type\r\n« line. If $size  is  »‐1«,  then  the  client
60 should  listen  until the connection is closed. When clients are sending
61 data, then they are required to specify a valid size.
63 Adding  another »\t« to the request allows the client to specify a range
64 of bytes to be retrieved. See the section about ranges for  further  de‐
65 tails.
67 ### Pipelining
69 Every  connection  to  a server could be used for many requests. Servers
70 should implement this, but can apply rules upon how  many  requests  and
71 what requests are allowed when pipelining multiple requests.
73 It is possible for simple servers to close the connection after each re‐
74 quest. Simple clients can close the  connection  after  simple  requests
75 too.  This  may  disable  certain interactive sessions for these kind of
76 clients.
78 One exception in pipelining is when a selector is has an undefined size.
79 Then the connection must be closed after all data has been sent  to  the
80 client.  Both  sides  of the connection can close the connection, when a
81 certain rule maximum of bytes is reached.
83 ### Selectors
85 Selectors   are  UTF‐8  strings.  They must not contain the »\0« or »\t«
86 charac‐ ters, to avoid parsing errors. Servers need »\t«  for  determin‐
87 ing,   if   the request is a complex request. See the sections below for
88 clarification.
90 Selectors  must  use  »/« as path delimiter. This definition is required
91 for automagic file type recognition.
93 Relative  selectors  (containing a »..« or ».« path element) must not be
94 handled by a server and answered with an error.
96 ### Search
98 If  a  »search«  part is given in a request, then this is used when some
99 form of magic script will handle the answer. See the CGI section for how
100 to use it further.
102 ### Range
104 The default value of if »range« part is »‐«, which means to retrieve the
105 selector from its beginning to the end. If the »range« part has  a  zero
106 length, then the default value is assumed.
108         $beginning-$end
110 As seen, $beginning and $end can be of zero length and will be replaced
111 by »0« ($beginning) or the size of the selector data ($end).
113 Both, $beginning and $end must be positive natural numbers.
115         -$end
116         $beginning-
118 These  are  possible  formats and mean, that the client is requesting in
119 the first case the byte range from the first byte of the data  (byte  0)
120 to  byte  $end; including byte $end. The second example will send to the
121 client byte $beginning to the end of the data.
123 Multiple byte ranges are best served using pipelining and multiple range
124 requests. There is no support for them in one request.
126 If a range cannot be satisfied, see the section about Error Handling how
127 to handle this.
129 A range of zero bytes can be used to determine the size and mime‐type of
130 a selector.
132 ### Mime-Type
134 There are some special MIME types required for this document.
136         text/x-menu – menu
137         application/x-interactive – interactive 
139 The  short forms of menu item types must not be used when sending or re‐
140 questing a selector.
142 ## Menu
144 Each  menu  item  consists of one line. The line must be valid UTF‐8 and
145 consists of the following form.
147         $mime-type\t$name\t$selector\t$searchstring\t$host\t$port\r\n
149 $mime‐type  and  $name are mandatory for every line. The other parts can
150 be left out, if unspecified and will result in the below  specified  de‐
151 faults  to  be  applied. If a menu type does not have any use for a menu
152 item part, then it can be left out and ignored.
154 For $mime-type see the section about them below.
156 $name is referring to the text that should be displayed at this position
157 in the menu.
159 $selector specifies the selector that should be sent to the server, then
160 this is a line pointing back to a  server  implementing  this  protocol.
161 See the different menu item types below for exceptions.
163 $searchstring is what should be sent to the server in a complex query as
164 the »search« part. This implies the  client  to  implement  complex  re‐
165 quests.
167 $host  and  $port  are used when this item type is referring to a server
168 implementing this protocol. If they are empty, then  the  host  or  port
169 used to retrieve this menu is used as a replacement.
171 ### Menu Item MIME types
173 The  used  $mime‐type fields in this protocol can refer not just to MIME
174 types. The history of gopher has shown short item types to be more prac‐
175 tical,  but  insufficent to handle modern needs. This proposal here is a
176 mix out of both worlds.
178 If  the  length  of  the $mime‐type is just one char, then the following
179 item types are assumed:
181         i – informational
182         m – menu
183         s – interactive
184         u – uri
185         e – error
186         b – binary
188 If the $mime‐type is longer than one char, then this is a true MIME type
189 and will be used like that.
191 #### Informational
193 This type is describing text in the $name field that should be displayed
194 at that position in the menu.
196 #### Menu 
198 The selector of this item will lead to another menu.
200 When requesting this selector, then the $mime‐type of »text/x‐menu« must
201 be used.
203 #### Interactive 
205 See the section about search / interactive sessions how to handle these.
207 When requesting this selector, then the $mime‐type of »application/x‐in‐
208 teractive« must be used.
211 #### Uri 
213 When  displaying  this  menu item issuing/clicking it should lead to the
214 URI mentioned in the $selector field and the text  in  the  $name  field
215 should  be displayed as a description. Handling URIs must be done on the
216 client side.
218 #### Error 
220 This  menu  item  is  describing  an error message. The item must be the
221 first item in the menu. See the section on error handling for  more  de‐
222 tails.
224 #### Binary
226 Implementing this menu item can be done on server or client side. If the
227 server supports this, then it should parse the menus before sending them
228 and  try to determine the real MIME type of the file the selector is re‐
229 ferring to. If this is not possible, then the client has to decide.
231 If  the  client  is  supporting this menu type and the »b« type appears,
232 then it should assume this to be of type »application/octet‐stream«  and
233 simply store the file.
235 ### Search / Interactive
237 The client is retrieving the selector where the interactive sessions be‐
238 gins:
240         C->S: /somemenu\r\n
241         S->C: [...]
242               s\tPlease Register!\t/register\r\n
244 A  client  not  willing to interpret this as an interactive session will
245 just retrieve the raw data. Clients willing to  interact  interpret  the
246 following in a new request:
248         C->S: /register\r\n
249         S->C: $size\ts\r\n
250               s\tNext register step\t/register2\t$metadata\r\n
251               Username: \n
252               Password: \n
253               E-Mail: \n
254         [The user is now shown its favourite text editor, which shows
255          all the given back text, except for the first line. ]
257 The  first  line  of  the output is a menu item, which the client should
258 follow after the user applied its changed to the data below it.
260 Data  following this first line can be binary and application‐dependent,
261 but it must be valid UTF‐8, if it is text. See the  section  about  sim‐
262 plicity below for how to handle this.
264 When  showing  the  data  to the user and it is a text interaction, then
265 filters could apply on the application side or the client side,  to  add
266 some  more descriptive text, that tells the user, that it is possible to
267 abort everything by changing nothing and just leaving the editor.  Other
268 binary applications may have different rules.
270 Base  on  the  example, another sub standard could be used to assume the
271 form of »Field: Value« to be a form entry, which  is  shown  in  a  more
272 graphical way, so users won’t change the key text.
274 Clients  should  apply  filters on redirects to menu items away from the
275 server the interactive session started. It is adviced to not follow them
276 by default.
278 The answer could be:
280         C->S: /register2\t$metadata\r\n
281               $size\ts\r\n
282               Username: fee\n
283               Password: bar\n
284               E-Mail: me@somewhere.com\n
285         S->C: $size\tm\r\n
286               i\tWelcome to this service!\r\n
287               m\tNext >>\t/\r\n
289 When  sending the answer, the client is not sending a line for the redi‐
290 rect. It is sending back the changed data from the user.
292 Interactive  sessions may end from the client or server side. The server
293 side must guide the user to a sane end whenever possible.
295 ### Simplicity
297 Authors   creating interactive sessions are encouraged to keep them sim‐
298 ple.  It should be possible to access them using commandline tools.  The
299 best way to ensure this is to keep all interaction in valid UTF‐8 and an
300 understandable form. Binary formats make things hard to  parse  and  use
301 without specialized tools.
303 ## Error Handling
305 When  an  error  occurs, then the server is sending back a menu with the
306 first element being of type »e«.
308 Upon errors the server is not required to close the connection, if it is
309 not a fatal error that would prevent further serving.
311 Error  messages  must  not be localized and should contain at least some
312 standard messages. The parsing is done using simple string operations.
314 Some predefined error messages that should be used are:
316         Selector not found
317         Permission denied
318         Range cannot be satisfied
319         Unhandled CGI error
320         Malformed request
322 If the server encounters a relative selector:
324         Relative selectors are not allowed
326 When the client did not send enough data:
328         Wrong size
330 The chameleon of error messages is
332         Internal server error
334 Sending  out  this error message is allowed but should be avoided at all
335 costs.
337 Example:
339         C->S: /someselector\r\n
340         S->C: $size\tm\r\n
341               e\tFile not found\r\n
342               m\tBack to the index\t/\r\n
344 ## File Types
346 If  just  a server and selector is given to a client and it tries to de‐
347 termine how to handle the data behind this selector, following algorithm
348 should be used.
350 1. See if the mime-type is cached somewhere.
351 2. If the selector is the root of the server, just fetch it and abort,
352    if this is not a type of »m«.
353 3. Use a request with the range of »0-0« to determine the MIME type
354    and size.
355 4. If the MIME type is still »b«, then assume »application/octet-stream«.
358 ## CGI
360 If  scripts  are called within a server or a request the following envi‐
361 ronment variables should be provided in the  operating  system  specific
362 way.
364 By  default  all  variables  are  unset. Only if relevant information is
365 available they should be set.
367 The  value  of  these variables should be in UTF‐8, unless the operating
368 system would make using this default impractical. Then the possible  de‐
369 velopers must be informed about this new assumption.
371 ### CONTENT_LENGTH
373 The size of the data sent by the client.
375 ### CONTENT_TYPE
377 The  mime‐type the client sent for the request. The default mediatype is
378 UTF‐8. This incompatibility to HTTP CGI is intentional.
380 ### GATEWAY_INTERFACE
382 »CGI/1.1«. This is not really used.
384 ### PATH_INFO
386 This is the selector given by the client.
388 ### PATH_TRANSLATED
390 This  si the PATH_INFO mapped to the storage where the server is getting
391 the script from. It does not need to be a valid physical path.
393 ### QUERY_STRING
395 This should be the »search« part of a complex request.
397 ### REMOTE_ADDR
399 This  must be the address of the client requesting an answer in the com‐
400 mon used representation for such an address. If the protocol has no  no‐
401 tion of an address, it can be left blank.
403 ### REMOTE_HOST
405 REMOTE_HOST  can  contain  the fually qualified host of the REMOTE_ADDR.
406 The limitations of the used protocol are applied,  so  it  can  be  left
407 blank.
409 ### REQUEST_METHOD
411 If  the client is providing data, then this is seen as a »POST« request.
412 If no data is provided, then it is a »GET« request.
415 ### SCRIPT_NAME
417 This is the filename or leading path component of the PATH_INFO.
419 ### SERVER_NAME
421 SERVER_NAME  is  the  hostname or address of the server. If the protocol
422 this protocol is used on has no notion of address, it must be set  to  a
423 funny name.
425 ### SERVER_PORT
427 This  is  the  port on which the request was received. It should be left
428 blank, if no port notion is known in the underlying protocol.
430 ### SERVER_PROTOCOL
432 »GOPHER/3.0«
434 ### SERVER_SOFTWARE
436 This  variable describes the server and must include the server software
437 name and version. If no version is known, a funny name must be added.