home *** CD-ROM | disk | FTP | other *** search
/ Internet Info 1997 December / Internet_Info_CD-ROM_Walnut_Creek_December_1997.iso / drafts / draft_n_r / draft-newman-url-imap-09.txt < prev    next >
Text File  |  1997-05-22  |  32KB  |  901 lines

  1.  
  2.  
  3.  
  4.  
  5.  
  6.  
  7. Network Working Group                                          C. Newman
  8. Internet Draft: IMAP URL Scheme                                 Innosoft
  9. Document: draft-newman-url-imap-09.txt                          May 1997
  10.                                                    Expires in six months
  11.  
  12.  
  13.                             IMAP URL Scheme
  14.  
  15.  
  16. Status of this memo
  17.  
  18.      This document is an Internet Draft.  Internet Drafts are working
  19.      documents of the Internet Engineering Task Force (IETF), its Areas,
  20.      and its Working Groups.  Note that other groups may also distribute
  21.      working documents as Internet Drafts.
  22.  
  23.      Internet Drafts are draft documents valid for a maximum of six
  24.      months.  Internet Drafts may be updated, replaced, or obsoleted by
  25.      other documents at any time.  It is not appropriate to use Internet
  26.      Drafts as reference material or to cite them other than as a
  27.      ``working draft'' or ``work in progress``.
  28.  
  29.      To learn the current status of any Internet-Draft, please check the
  30.      1id-abstracts.txt listing contained in the Internet-Drafts Shadow
  31.      Directories on ds.internic.net, nic.nordu.net, ftp.isi.edu, or
  32.      munnari.oz.au.
  33.  
  34.      A revised version of this draft document will be submitted to the
  35.      RFC editor as a Proposed Standard for the Internet Community.
  36.      Discussion and suggestions for improvement are requested.  This
  37.      document will expire six months after publication.  Distribution of
  38.      this draft is unlimited.
  39.  
  40.  
  41. Abstract
  42.  
  43.      IMAP [IMAP4] is a rich protocol for accessing remote message
  44.      stores.  It provides an ideal mechanism for accessing public
  45.      mailing list archives as well as private and shared message stores.
  46.      This document defines a URL scheme for referencing objects on an
  47.      IMAP server.
  48.  
  49.  
  50. 1. Conventions used in this document
  51.  
  52.      The key words "MUST", "MUST NOT", "SHOULD", "SHOULD NOT", and "MAY"
  53.      in this document are to be interpreted as defined in "Key words for
  54.      use in RFCs to Indicate Requirement Levels" [KEYWORDS].
  55.  
  56.  
  57.  
  58. Newman                                                          [Page 1]
  59.  
  60. Internet Draft              IMAP URL Scheme                     May 1997
  61.  
  62.  
  63. 2. IMAP scheme
  64.  
  65.      The IMAP URL scheme is used to designate IMAP servers, mailboxes,
  66.      messages, MIME bodies [MIME], and search programs on Internet hosts
  67.      accessible using the IMAP protocol.
  68.  
  69.      The IMAP URL follows the common Internet scheme syntax as defined
  70.      in RFC 1738 [BASIC-URL] except that clear text passwords are not
  71.      permitted.  If :<port> is omitted, the port defaults to 143.
  72.  
  73.      An IMAP URL takes one of the following forms:
  74.  
  75.          imap://<iserver>/
  76.          imap://<iserver>/<enc_list_mailbox>;TYPE=<list_type>
  77.          imap://<iserver>/<enc_mailbox>[uidvalidity][?<enc_search>]
  78.          imap://<iserver>/<enc_mailbox>[uidvalidity]<iuid>[isection]
  79.  
  80.      The first form is used to refer to an IMAP server, the second form
  81.      refers to a list of mailboxes, the third form refers to the
  82.      contents of a mailbox or a set of messages resulting from a search,
  83.      and the final form refers to a specific message or message part.
  84.      Note that the syntax here is informal.  The authoritative formal
  85.      syntax for IMAP URLs is defined in section 11.
  86.  
  87.  
  88. 3. IMAP User Name and Authentication Mechanism
  89.  
  90.      A user name and/or authentication mechanism may be supplied.  They
  91.      are used in the "LOGIN" or "AUTHENTICATE" commands after making the
  92.      connection to the IMAP server.  If no user name or authentication
  93.      mechanism is supplied, the user name "anonymous" is used with the
  94.      "LOGIN" command and the password is supplied as the Internet e-mail
  95.      address of the end user accessing the resource.  If the URL doesn't
  96.      supply a user name, the program interpreting the IMAP URL SHOULD
  97.      request one from the user if necessary.
  98.  
  99.      An authentication mechanism can be expressed by adding
  100.      ";AUTH=<enc_auth_type>" to the end of the user name.  When such an
  101.      <enc_auth_type> is indicated, the client SHOULD request appropriate
  102.      credentials from that mechanism and use the "AUTHENTICATE" command
  103.      instead of the "LOGIN" command.  If no user name is specified, one
  104.      SHOULD be obtained from the mechanism or requested from the user as
  105.      appropriate.
  106.  
  107.      The string ";AUTH=*" indicates that the client SHOULD select an
  108.      appropriate authentication mechanism.  It MAY use any mechanism
  109.      listed in the CAPABILITY command or use an out of band security
  110.      service resulting in a PREAUTH connection.  If no user name is
  111.  
  112.  
  113.  
  114. Newman                                                          [Page 2]
  115.  
  116. Internet Draft              IMAP URL Scheme                     May 1997
  117.  
  118.  
  119.      specified and no appropriate authentication mechanisms are
  120.      available, the client SHOULD fall back to anonymous login as
  121.      described above.  This allows a URL which grants read-write access
  122.      to authorized users, and read-only anonymous access to other users.
  123.  
  124.      If a user name is included with no authentication mechanism, then
  125.      ";AUTH=*" is assumed.
  126.  
  127.      Since URLs can easily come from untrusted sources, care must be
  128.      taken when resolving a URL which requires or requests any sort of
  129.      authentication.  If authentication credentials are supplied to the
  130.      wrong server, it may compromise the security of the user's account.
  131.      The program resolving the URL should make sure it meets at least
  132.      one of the following criteria in this case:
  133.  
  134.      (1) The URL comes from a trusted source, such as a referral server
  135.      which the client has validated and trusts according to site policy.
  136.      Note that user entry of the URL may or may not count as a trusted
  137.      source, depending on the experience level of the user and site
  138.      policy.
  139.      (2) Explicit local site policy permits the client to connect to the
  140.      server in the URL.  For example, if the client knows the site
  141.      domain name, site policy may dictate that any hostname ending in
  142.      that domain is trusted.
  143.      (3) The user confirms that connecting to that domain name with the
  144.      specified credentials and/or mechanism is permitted.
  145.      (4) A mechanism is used which validates the server before passing
  146.      potentially compromising client credentials.
  147.      (5) An authentication mechanism is used which will not reveal
  148.      information to the server which could be used to compromise future
  149.      connections.
  150.  
  151.      URLs which do not include a user name must be treated with extra
  152.      care, since they are more likely to compromise the user's primary
  153.      account.  A URL containing ";AUTH=*" must also be treated with
  154.      extra care since it might fall back on a weaker security mechanism.
  155.      Finally, clients are discouraged from using a plain text password
  156.      as a fallback with ";AUTH=*" unless the connection has strong
  157.      encryption (e.g. a key length of greater than 56 bits).
  158.  
  159.      Note that if unsafe or reserved characters such as " " or ";" are
  160.      present in the user name or authentication mechanism, they MUST be
  161.      encoded as described in RFC 1738 [BASIC-URL].
  162.  
  163.  
  164.  
  165.  
  166.  
  167.  
  168.  
  169.  
  170. Newman                                                          [Page 3]
  171.  
  172. Internet Draft              IMAP URL Scheme                     May 1997
  173.  
  174.  
  175. 4. IMAP server
  176.  
  177.      An IMAP URL referring to an IMAP server has the following form:
  178.  
  179.          imap://<iserver>/
  180.  
  181.      A program interpreting this URL would issue the standard set of
  182.      commands it uses to present a view of the contents of an IMAP
  183.      server.  This is likely to be semanticly equivalent to one of the
  184.      following URLs:
  185.  
  186.          imap://<iserver>/;TYPE=LIST
  187.          imap://<iserver>/;TYPE=LSUB
  188.  
  189.      The program interpreting this URL SHOULD use the LSUB form if it
  190.      supports mailbox subscriptions.
  191.  
  192.  
  193. 5. Lists of mailboxes
  194.  
  195.      An IMAP URL referring to a list of mailboxes has the following
  196.      form:
  197.  
  198.          imap://<iserver>/<enc_list_mailbox>;TYPE=<list_type>
  199.  
  200.      The <list_type> may be either "LIST" or "LSUB", and is case
  201.      insensitive.  The field ";TYPE=<list_type>" MUST be included.
  202.  
  203.      The <enc_list_mailbox> is any argument suitable for the
  204.      list_mailbox field of the IMAP [IMAP4] LIST or LSUB commands.  The
  205.      field <enc_list_mailbox> may be omitted, in which case the program
  206.      interpreting the IMAP URL may use "*" or "%" as the
  207.      <enc_list_mailbox>.  The program SHOULD use "%" if it supports a
  208.      hierarchical view, otherwise it SHOULD use "*".
  209.  
  210.      Note that if unsafe or reserved characters such as " " or "%" are
  211.      present in <enc_list_mailbox> they MUST be encoded as described in
  212.      RFC 1738 [BASIC-URL].  If the character "/" is present in
  213.      enc_list_mailbox, it SHOULD NOT be encoded.
  214.  
  215.  
  216. 6. Lists of messages
  217.  
  218.      An IMAP URL referring to a list of messages has the following form:
  219.  
  220.          imap://<iserver>/<enc_mailbox>[uidvalidity][?<enc_search>]
  221.  
  222.      The <enc_mailbox> field is used as the argument to the IMAP4
  223.  
  224.  
  225.  
  226. Newman                                                          [Page 4]
  227.  
  228. Internet Draft              IMAP URL Scheme                     May 1997
  229.  
  230.  
  231.      "SELECT" command.  Note that if unsafe or reserved characters such
  232.      as " ", ";", or "?" are present in <enc_mailbox> they MUST be
  233.      encoded as described in RFC 1738 [BASIC-URL].  If the character "/"
  234.      is present in enc_mailbox, it SHOULD NOT be encoded.
  235.  
  236.      The [uidvalidity] field is optional.  If it is present, it MUST be
  237.      the argument to the IMAP4 UIDVALIDITY status response at the time
  238.      the URL was created.  This SHOULD be used by the program
  239.      interpreting the IMAP URL to determine if the URL is stale.
  240.  
  241.      The [?<enc_search>] field is optional.  If it is not present, the
  242.      contents of the mailbox SHOULD be presented by the program
  243.      interpreting the URL.  If it is present, it SHOULD be used as the
  244.      arguments following an IMAP4 SEARCH command with unsafe characters
  245.      such as " " (which are likely to be present in the <enc_search>)
  246.      encoded as described in RFC 1738 [BASIC-URL].
  247.  
  248.  
  249. 7. A specific message or message part
  250.  
  251.      An IMAP URL referring to a specific message or message part has the
  252.      following form:
  253.  
  254.          imap://<iserver>/<enc_mailbox>[uidvalidity]<iuid>[isection]
  255.  
  256.      The <enc_mailbox> and [uidvalidity] are as defined above.
  257.  
  258.      If [uidvalidity] is present in this form, it SHOULD be used by the
  259.      program interpreting the URL to determine if the URL is stale.
  260.  
  261.      The <iuid> refers to an IMAP4 message UID, and SHOULD be used as
  262.      the <set> argument to the IMAP4 "UID FETCH" command.
  263.  
  264.      The [isection] field is optional.  If not present, the URL refers
  265.      to the entire Internet message as returned by the IMAP command "UID
  266.      FETCH <uid> BODY.PEEK[]".  If present, the URL refers to the object
  267.      returned by a "UID FETCH <uid> BODY.PEEK[<section>]" command.  The
  268.      type of the object may be determined with a "UID FETCH <uid>
  269.      BODYSTRUCTURE" command and locating the appropriate part in the
  270.      resulting BODYSTRUCTURE.  Note that unsafe characters in [isection]
  271.      MUST be encoded as described in [BASIC-URL].
  272.  
  273.  
  274. 8. Relative IMAP URLs
  275.  
  276.      Relative IMAP URLs are permitted and are resolved according to the
  277.      rules defined in RFC 1808 [REL-URL] with one exception.  In IMAP
  278.      URLs, parameters are treated as part of the normal path with
  279.  
  280.  
  281.  
  282. Newman                                                          [Page 5]
  283.  
  284. Internet Draft              IMAP URL Scheme                     May 1997
  285.  
  286.  
  287.      respect to relative URL resolution.  This is believed to be the
  288.      behavior of the installed base and is likely to be documented in a
  289.      future revision of the relative URL specification.
  290.  
  291.      The following observations are also important:
  292.  
  293.      The <iauth> grammar element is considered part of the user name for
  294.      purposes of resolving relative IMAP URLs.  This means that unless a
  295.      new login/server specification is included in the relative URL, the
  296.      authentication mechanism is inherited from a base IMAP URL.
  297.  
  298.      URLs always use "/" as the hierarchy delimiter for the purpose of
  299.      resolving paths in relative URLs.  IMAP4 permits the use of any
  300.      hierarchy delimiter in mailbox names.  For this reason, relative
  301.      mailbox paths will only work if the mailbox uses "/" as the
  302.      hierarchy delimiter.  Relative URLs may be used on mailboxes which
  303.      use other delimiters, but in that case, the entire mailbox name
  304.      MUST be specified in the relative URL or inherited as a whole from
  305.      the base URL.
  306.  
  307.      The base URL for a list of mailboxes or messages which was referred
  308.      to by an IMAP URL is always the referring IMAP URL itself.  The
  309.      base URL for a message or message part which was referred to by an
  310.      IMAP URL may be more complicated to determine.  The program
  311.      interpreting the relative URL will have to check the headers of the
  312.      MIME entity and any enclosing MIME entities in order to locate the
  313.      "Content-Base" and "Content-Location" headers.  These headers are
  314.      used to determine the base URL as defined in [HTTP].  For example,
  315.      if the referring IMAP URL contains a "/;SECTION=1.2" parameter,
  316.      then the MIME headers for section 1.2, for section 1, and for the
  317.      enclosing message itself SHOULD be checked in that order for
  318.      "Content-Base" or "Content-Location" headers.
  319.  
  320.  
  321. 9. Multinational Considerations
  322.  
  323.      IMAP4 [IMAP4] section 5.1.3 includes a convention for encoding
  324.      non-US-ASCII characters in IMAP mailbox names.  Because this
  325.      convention is private to IMAP, it is necessary to convert IMAP's
  326.      encoding to one that can be more easily interpreted by a URL
  327.      display program.  For this reason, IMAP's modified UTF-7 encoding
  328.      for mailboxes MUST be converted to UTF-8 [UTF8].  Since 8-bit
  329.      characters are not permitted in URLs, the UTF-8 characters are
  330.      encoded as required by the URL specification [BASIC-URL].  Sample
  331.      code is included in Appendix A to demonstrate this conversion.
  332.  
  333.  
  334.  
  335.  
  336.  
  337.  
  338. Newman                                                          [Page 6]
  339.  
  340. Internet Draft              IMAP URL Scheme                     May 1997
  341.  
  342.  
  343. 10. Examples
  344.  
  345.      The following examples demonstrate how an IMAP4 client program
  346.      might translate various IMAP4 URLs into a series of IMAP4 commands.
  347.      Commands sent from the client to the server are prefixed with "C:",
  348.      and responses sent from the server to the client are prefixed with
  349.      "S:".
  350.  
  351.      The URL:
  352.  
  353.       <imap://minbari.org/gray-council;UIDVALIDITY=385759045/;UID=20>
  354.  
  355.      Results in the following client commands:
  356.  
  357.          <connect to minbari.org, port 143>
  358.          C: A001 LOGIN ANONYMOUS sheridan@babylon5.org
  359.          C: A002 SELECT gray-council
  360.          <client verifies the UIDVALIDITY matches>
  361.          C: A003 UID FETCH 20 BODY.PEEK[]
  362.  
  363.      The URL:
  364.  
  365.       <imap://michael@minbari.org/users.*;type=list>
  366.  
  367.      Results in the following client commands:
  368.  
  369.          <client requests password from user>
  370.          <connect to minbari.org imap server, activate strong encryption>
  371.          C: A001 LOGIN MICHAEL zipper
  372.          C: A002 LIST "" users.*
  373.  
  374.      The URL:
  375.  
  376.       <imap://psy.earth/~peter/%E6%97%A5%E6%9C%AC%E8%AA%9E/%E5%8F%B0%E5%8C%97>
  377.  
  378.      Results in the following client commands:
  379.  
  380.          <connect to psy.earth, port 143>
  381.          C: A001 LOGIN ANONYMOUS bester@psycop.psy.earth
  382.          C: A002 SELECT ~peter/&ZeVnLIqe-/&U,BTFw-
  383.          <commands the client uses for viewing the contents of a mailbox>
  384.  
  385.  
  386.  
  387.  
  388.  
  389.  
  390.  
  391.  
  392.  
  393.  
  394. Newman                                                          [Page 7]
  395.  
  396. Internet Draft              IMAP URL Scheme                     May 1997
  397.  
  398.  
  399.      The URL:
  400.  
  401.       <imap://;AUTH=KERBEROS_V4@minbari.org/gray-council/;uid=20/;section=1.2>
  402.  
  403.      Results in the following client commands:
  404.  
  405.          <connect to minbari.org, port 143>
  406.          C: A001 AUTHENTICATE KERBEROS_V4
  407.          <authentication exchange>
  408.          C: A002 SELECT gray-council
  409.          C: A003 UID FETCH 20 BODY.PEEK[1.2]
  410.  
  411.      If the following relative URL is located in that body part:
  412.  
  413.       <;section=1.4>
  414.  
  415.      This could result in the following client commands:
  416.  
  417.          C: A004 UID FETCH 20 (BODY.PEEK[1.2.MIME]
  418.                  BODY.PEEK[1.MIME]
  419.                  BODY.PEEK[HEADER.FIELDS (Content-Base Content-Location)])
  420.          <Client looks for Content-Base or Content-Location headers in
  421.           result.  If no such headers, then it does the following>
  422.          C: A005 UID FETCH 20 BODY.PEEK[1.4]
  423.  
  424.      The URL:
  425.  
  426.       <imap://;AUTH=*@minbari.org/gray%20council?SUBJECT%20shadows>
  427.  
  428.      Could result in the following:
  429.  
  430.          <connect to minbari.org, port 143>
  431.          C: A001 CAPABILITY
  432.          S: * CAPABILITY IMAP4rev1 AUTH=GSSAPI
  433.          S: A001 OK
  434.          C: A002 AUTHENTICATE GSSAPI
  435.          <authentication exchange>
  436.          S: A002 OK user lennier authenticated
  437.          C: A003 SELECT "gray council"
  438.          ...
  439.          C: A004 SEARCH SUBJECT shadows
  440.          S: * SEARCH 8 10 13 14 15 16
  441.          S: A004 OK SEARCH completed
  442.          C: A005 FETCH 8,10,13:16 ALL
  443.          ...
  444.  
  445.      NOTE: In this final example, the client has implementation dependent
  446.      choices.  The authentication mechanism could be anything, including
  447.  
  448.  
  449.  
  450. Newman                                                          [Page 8]
  451.  
  452. Internet Draft              IMAP URL Scheme                     May 1997
  453.  
  454.  
  455.      PREAUTH.  And the final FETCH command could fetch more or less
  456.      information about the messages, depending on what it wishes to display
  457.      to the user.
  458.  
  459.  
  460. 11. Security Considerations
  461.  
  462.      Security considerations discussed in the IMAP specification [IMAP4]
  463.      and the URL specification [BASIC-URL] are relevant.  Security
  464.      considerations related to authenticated URLs are discussed in
  465.      section 3 of this document.
  466.  
  467.      Many email clients store the plain text password for later use
  468.      after logging into an IMAP server.  Such clients MUST NOT use a
  469.      stored password in response to an IMAP URL without explicit
  470.      permission from the user to supply that password to the specified
  471.      host name.
  472.  
  473.  
  474. 12. ABNF for IMAP URL scheme
  475.  
  476.      This uses ABNF as defined in RFC 822 [IMAIL].  Terminals from the
  477.      BNF for IMAP [IMAP4] and URLs [BASIC-URL] are also used.  Strings
  478.      are not case sensitive and free insertion of linear-white-space is
  479.      not permitted.
  480.  
  481.      achar            = uchar / "&" / "=" / "~"
  482.                              ; see [BASIC-URL] for "uchar" definition
  483.  
  484.      bchar            = achar / ":" / "@" / "/"
  485.  
  486.      enc_auth_type    = 1*achar
  487.                              ; encoded version of [IMAP-AUTH] "auth_type"
  488.  
  489.      enc_list_mailbox = 1*bchar
  490.                              ; encoded version of [IMAP4] "list_mailbox"
  491.  
  492.      enc_mailbox      = 1*bchar
  493.                              ; encoded version of [IMAP4] "mailbox"
  494.  
  495.      enc_search       = 1*bchar
  496.                              ; encoded version of search_program below
  497.  
  498.      enc_section      = 1*bchar
  499.                              ; encoded version of section below
  500.  
  501.      enc_user         = 1*achar
  502.                              ; encoded version of [IMAP4] "userid"
  503.  
  504.  
  505.  
  506. Newman                                                          [Page 9]
  507.  
  508. Internet Draft              IMAP URL Scheme                     May 1997
  509.  
  510.  
  511.      imapurl          = "imap://" iserver "/" [ icommand ]
  512.  
  513.      iauth            = ";AUTH=" ( "*" / enc_auth_type )
  514.  
  515.      icommand         = imailboxlist / ipath / isearch
  516.  
  517.      imailboxlist     = [enc_list_mailbox] ";TYPE=" list_type
  518.  
  519.      ipath            = enc_mailbox [uidvalidity] iuid [isection]
  520.  
  521.      isearch          = enc_mailbox [ "?" enc_search ] [uidvalidity]
  522.  
  523.      isection         = "/;SECTION=" enc_section
  524.  
  525.      iserver          = [iuserauth "@"] hostport
  526.                              ; See [BASIC-URL] for "hostport" definition
  527.  
  528.      iuid             = "/;UID=" nz_number
  529.                              ; See [IMAP4] for "nz_number" definition
  530.  
  531.      iuserauth        = enc_user [iauth] / [enc_user] iauth
  532.  
  533.      list_type        = "LIST" / "LSUB"
  534.  
  535.      search_program   = ["CHARSET" SPACE astring SPACE] 1#search_key
  536.                              ; IMAP4 literals may not be used
  537.                              ; See [IMAP4] for "astring" and "search_key"
  538.  
  539.      section          = section_text / (nz_number *["." nz_number]
  540.                          ["." (section_text / "MIME")])
  541.                              ; See [IMAP4] for "section_text" and "nz_number"
  542.  
  543.      uidvalidity      = ";UIDVALIDITY=" nz_number
  544.                              ; See [IMAP4] for "nz_number" definition
  545.  
  546.  
  547. 13. References
  548.  
  549.      [BASIC-URL] Berners-Lee, Masinter, McCahill, "Uniform Resource
  550.      Locators (URL)", RFC 1738, CERN, Xerox Corporation, University of
  551.      Minnesota, December 1994.
  552.  
  553.          <ftp://ds.internic.net/rfc/rfc1738.txt>
  554.  
  555.      [IMAP4] Crispin, M., "Internet Message Access Protocol - Version
  556.      4rev1", RFC 2060, University of Washington, December 1996.
  557.  
  558.          <ftp://ds.internic.net/rfc/rfc2060.txt>
  559.  
  560.  
  561.  
  562. Newman                                                         [Page 10]
  563.  
  564. Internet Draft              IMAP URL Scheme                     May 1997
  565.  
  566.  
  567.      [IMAP-AUTH] Myers, J., "IMAP4 Authentication Mechanism", RFC 1731,
  568.      Carnegie-Mellon University, December 1994.
  569.  
  570.          <ftp://ds.internic.net/rfc/rfc1731.txt>
  571.  
  572.      [HTTP] Fielding, Gettys, Mogul, Frystyk, Berners-Lee, "Hypertext
  573.      Transfer Protocol -- HTTP/1.1", RFC 2068, UC Irvine, DEC, MIT/LCS,
  574.      January 1997.
  575.  
  576.          <ftp://ds.internic.net/rfc/rfc2068.txt>
  577.  
  578.      [IMAIL] Crocker, "Standard for the Format of ARPA Internet Text
  579.      Messages", STD 11, RFC 822, University of Delaware, August 1982.
  580.  
  581.          <ftp://ds.internic.net/rfc/rfc822.txt>
  582.  
  583.      [KEYWORDS] Bradner, "Key words for use in RFCs to Indicate
  584.      Requirement Levels", RFC 2119, Harvard University, March 1997.
  585.  
  586.          <ftp://ds.internic.net/rfc/rfc2119.txt>
  587.  
  588.      [MIME] Freed, N., Borenstein, N., "Multipurpose Internet Mail
  589.      Extensions", RFC 2045, Innosoft, First Virtual, November 1996.
  590.  
  591.         <ftp://ds.internic.net/rfc/rfc2045.txt>
  592.  
  593.      [REL-URL] Fielding, "Relative Uniform Resource Locators", RFC 1808,
  594.      UC Irvine, June 1995.
  595.  
  596.          <ftp://ds.internic.net/rfc/rfc1808.txt>
  597.  
  598.      [UTF8] Yergeau, F. "UTF-8, a transformation format of Unicode and
  599.      ISO 10646", RFC 2044, Alis Technologies, October 1996.
  600.  
  601.          <ftp://ds.internic.net/rfc/rfc2044.txt>
  602.  
  603.  
  604. 14. Author's Address
  605.  
  606.      Chris Newman
  607.      Innosoft International, Inc.
  608.      1050 East Garvey Ave. South
  609.      West Covina, CA 91790 USA
  610.  
  611.      Email: chris.newman@innosoft.com
  612.  
  613.  
  614.  
  615.  
  616.  
  617.  
  618. Newman                                                         [Page 11]
  619.  
  620. Internet Draft              IMAP URL Scheme                     May 1997
  621.  
  622.  
  623. Appendix A.  Sample code
  624.  
  625. Here is sample C source code to convert between URL paths and IMAP
  626. mailbox names, taking into account mapping between IMAP's modified UTF-7
  627. [IMAP4] and hex-encoded UTF-8 which is more appropriate for URLs.  This
  628. code has not been rigorously tested nor does it necessarily behave
  629. reasonably with invalid input, but it should serve as a useful example.
  630. This code just converts the mailbox portion of the URL and does not deal
  631. with parameters, query or server components of the URL.
  632.  
  633. #include <stdio.h>
  634. #include <string.h>
  635.  
  636. /* hexadecimal lookup table */
  637. static char hex[] = "0123456789ABCDEF";
  638.  
  639. /* URL unsafe printable characters */
  640. static char urlunsafe[] = " \"#%&+:;<=>?@[\\]^`{|}";
  641.  
  642. /* UTF7 modified base64 alphabet */
  643. static char base64chars[] =
  644.   "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+,";
  645. #define UNDEFINED 64
  646.  
  647. /* UTF16 definitions */
  648. #define UTF16MASK       0x03FFUL
  649. #define UTF16SHIFT      10
  650. #define UTF16HIGHSTART  0xD800UL
  651. #define UTF16HIGHEND    0xDBFFUL
  652. #define UTF16LOSTART    0xDC00UL
  653. #define UTF16LOEND      0xDFFFUL
  654.  
  655. /* Convert an IMAP mailbox to a URL path
  656.  *  dst needs to have roughly 4 times the storage space of src
  657.  *    Hex encoding can triple the size of the input
  658.  *    UTF-7 can be slightly denser than UTF-8
  659.  *     (worst case: 8 octets UTF-7 becomes 9 octets UTF-8)
  660.  */
  661. void MailboxToURL(char *dst, char *src)
  662. {
  663.     unsigned char c, i, bitcount;
  664.     unsigned long ucs4, utf16, bitbuf;
  665.     unsigned char base64[256], utf8[6];
  666.  
  667.     /* initialize modified base64 decoding table */
  668.     memset(base64, UNDEFINED, sizeof (base64));
  669.     for (i = 0; i < sizeof (base64chars); ++i) {
  670.         base64[base64chars[i]] = i;
  671.  
  672.  
  673.  
  674. Newman                                                         [Page 12]
  675.  
  676. Internet Draft              IMAP URL Scheme                     May 1997
  677.  
  678.  
  679.     }
  680.  
  681.     /* loop until end of string */
  682.     while (*src != '\0') {
  683.         c = *src++;
  684.         /* deal with literal characters and &- */
  685.         if (c != '&' || *src == '-') {
  686.             if (c < ' ' || c > '~' || strchr(urlunsafe, c) != NULL) {
  687.                 /* hex encode if necessary */
  688.                 dst[0] = '%';
  689.                 dst[1] = hex[c >> 4];
  690.                 dst[2] = hex[c & 0x0f];
  691.                 dst += 3;
  692.             } else {
  693.                 /* encode literally */
  694.                 *dst++ = c;
  695.             }
  696.             /* skip over the '-' if this is an &- sequence */
  697.             if (c == '&') ++src;
  698.         } else {
  699.             /* convert modified UTF-7 -> UTF-16 -> UCS-4 -> UTF-8 -> HEX */
  700.             bitbuf = 0;
  701.             bitcount = 0;
  702.             ucs4 = 0;
  703.             while ((c = base64[(unsigned char) *src]) != UNDEFINED) {
  704.                 ++src;
  705.                 bitbuf = (bitbuf << 6) | c;
  706.                 bitcount += 6;
  707.                 /* enough bits for a UTF-16 character? */
  708.                 if (bitcount >= 16) {
  709.                     bitcount -= 16;
  710.                     utf16 = (bitcount ? bitbuf >> bitcount : bitbuf) & 0xffff;
  711.                     /* convert UTF16 to UCS4 */
  712.                     if (utf16 >= UTF16HIGHSTART && utf16 <= UTF16HIGHEND) {
  713.                         ucs4 = (utf16 & UTF16MASK) << UTF16SHIFT;
  714.                         continue;
  715.                     } else if (utf16 >= UTF16LOSTART && utf16 <= UTF16LOEND) {
  716.                         ucs4 |= utf16 & UTF16MASK;
  717.                     } else {
  718.                         ucs4 = utf16;
  719.                     }
  720.                     /* convert UTF-16 range of UCS4 to UTF-8 */
  721.                     if (ucs4 <= 0x7fUL) {
  722.                         utf8[0] = ucs4;
  723.                         i = 1;
  724.                     } else if (ucs4 <= 0x7ffUL) {
  725.                         utf8[0] = 0xc0 | (ucs4 >> 6);
  726.                         utf8[1] = 0x80 | (ucs4 & 0x3f);
  727.  
  728.  
  729.  
  730. Newman                                                         [Page 13]
  731.  
  732. Internet Draft              IMAP URL Scheme                     May 1997
  733.  
  734.  
  735.                         i = 2;
  736.                     } else if (ucs4 <= 0xffffUL) {
  737.                         utf8[0] = 0xe0 | (ucs4 >> 12);
  738.                         utf8[1] = 0x80 | ((ucs4 >> 6) & 0x3f);
  739.                         utf8[2] = 0x80 | (ucs4 & 0x3f);
  740.                         i = 3;
  741.                     } else {
  742.                         utf8[0] = 0xf0 | (ucs4 >> 18);
  743.                         utf8[1] = 0x80 | ((ucs4 >> 12) & 0x3f);
  744.                         utf8[2] = 0x80 | ((ucs4 >> 6) & 0x3f);
  745.                         utf8[3] = 0x80 | (ucs4 & 0x3f);
  746.                         i = 4;
  747.                     }
  748.                     /* convert utf8 to hex */
  749.                     for (c = 0; c < i; ++c) {
  750.                         dst[0] = '%';
  751.                         dst[1] = hex[utf8[c] >> 4];
  752.                         dst[2] = hex[utf8[c] & 0x0f];
  753.                         dst += 3;
  754.                     }
  755.                 }
  756.             }
  757.             /* skip over trailing '-' in modified UTF-7 encoding */
  758.             if (*src == '-') ++src;
  759.         }
  760.     }
  761.     /* terminate destination string */
  762.     *dst = '\0';
  763. }
  764.  
  765. /* Convert hex coded UTF-8 URL path to modified UTF-7 IMAP mailbox
  766.  *  dst should be about twice the length of src to deal with non-hex coded URLs
  767.  */
  768. void URLtoMailbox(char *dst, char *src)
  769. {
  770.     unsigned int utf8pos, utf8total, i, c, utf7mode, bitstogo, utf16flag;
  771.     unsigned long ucs4, bitbuf;
  772.     unsigned char hextab[256];
  773.  
  774.     /* initialize hex lookup table */
  775.     memset(hextab, 0, sizeof (hextab));
  776.     for (i = 0; i < sizeof (hex); ++i) {
  777.         hextab[hex[i]] = i;
  778.         if (isupper(hex[i])) hextab[tolower(hex[i])] = i;
  779.     }
  780.  
  781.     utf7mode = 0;
  782.     utf8total = 0;
  783.  
  784.  
  785.  
  786. Newman                                                         [Page 14]
  787.  
  788. Internet Draft              IMAP URL Scheme                     May 1997
  789.  
  790.  
  791.     bitstogo = 0;
  792.     while ((c = *src) != '\0') {
  793.         ++src;
  794.         /* undo hex-encoding */
  795.         if (c == '%' && src[0] != '\0' && src[1] != '\0') {
  796.             c = (hextab[src[0]] << 4) | hextab[src[1]];
  797.             src += 2;
  798.         }
  799.         /* normal character? */
  800.         if (c >= ' ' && c <= '~') {
  801.             /* switch out of UTF-7 mode */
  802.             if (utf7mode) {
  803.                 if (bitstogo) {
  804.                     *dst++ = base64chars[(bitbuf << (6 - bitstogo)) & 0x3F];
  805.                 }
  806.                 *dst++ = '-';
  807.                 utf7mode = 0;
  808.             }
  809.             *dst++ = c;
  810.             /* encode '&' as '&-' */
  811.             if (c == '&') {
  812.                 *dst++ = '-';
  813.             }
  814.             continue;
  815.         }
  816.         /* switch to UTF-7 mode */
  817.         if (!utf7mode) {
  818.             *dst++ = '&';
  819.             utf7mode = 1;
  820.         }
  821.         /* Encode US-ASCII characters as themselves */
  822.         if (c < 0x80) {
  823.             ucs4 = c;
  824.             utf8total = 1;
  825.         } else if (utf8total) {
  826.             /* save UTF8 bits into UCS4 */
  827.             ucs4 = (ucs4 << 6) | (c & 0x3FUL);
  828.             if (++utf8pos < utf8total) {
  829.                 continue;
  830.             }
  831.         } else {
  832.             utf8pos = 1;
  833.             if (c < 0xE0) {
  834.                 utf8total = 2;
  835.                 ucs4 = c & 0x1F;
  836.             } else if (c < 0xF0) {
  837.                 utf8total = 3;
  838.                 ucs4 = c & 0x0F;
  839.  
  840.  
  841.  
  842. Newman                                                         [Page 15]
  843.  
  844. Internet Draft              IMAP URL Scheme                     May 1997
  845.  
  846.  
  847.             } else {
  848.                 /* NOTE: can't convert UTF8 sequences longer than 4 */
  849.                 utf8total = 4;
  850.                 ucs4 = c & 0x03;
  851.             }
  852.             continue;
  853.         }
  854.         /* loop to split ucs4 into two utf16 chars if necessary */
  855.         utf8total = 0;
  856.         do {
  857.             if (ucs4 > 0xffffUL) {
  858.                 bitbuf = (bitbuf << 16) | ((ucs4 >> UTF16SHIFT)
  859.                                            + UTF16HIGHSTART);
  860.                 ucs4 = (ucs4 & UTF16MASK) + UTF16LOSTART;
  861.                 utf16flag = 1;
  862.             } else {
  863.                 bitbuf = (bitbuf << 16) | ucs4;
  864.                 utf16flag = 0;
  865.             }
  866.             bitstogo += 16;
  867.             /* spew out base64 */
  868.             while (bitstogo >= 6) {
  869.                 bitstogo -= 6;
  870.                 *dst++ = base64chars[(bitstogo ? (bitbuf >> bitstogo) : bitbuf)
  871.                                      & 0x3F];
  872.             }
  873.         } while (utf16flag);
  874.     }
  875.     /* if in UTF-7 mode, finish in ASCII */
  876.     if (utf7mode) {
  877.         if (bitstogo) {
  878.             *dst++ = base64chars[(bitbuf << (6 - bitstogo)) & 0x3F];
  879.         }
  880.         *dst++ = '-';
  881.     }
  882.     /* tie off string */
  883.     *dst = '\0';
  884. }
  885.  
  886.  
  887.  
  888.  
  889.  
  890.  
  891.  
  892.  
  893.  
  894.  
  895.  
  896.  
  897.  
  898. Newman                                                         [Page 16]
  899.  
  900.  
  901.