HTFormat.h
Upload User: zlh9724
Upload Date: 2007-01-04
Package Size: 1991k
Code Size: 13k
Category:

Browser Client

Development Platform:

Unix_Linux

  1. /*                                                                 Format Negotiation Manager
  2.                                     THE FORMAT MANAGER
  3.                                              
  4.  */
  5. /*
  6. **      (c) COPYRIGHT MIT 1995.
  7. **      Please first read the full copyright statement in the file COPYRIGH.
  8. */
  9. /*
  10.    Here we describe the functions of the HTFormat module which handles conversion between
  11.    different data representations. (In MIME parlance, a representation is known as a
  12.    content-type. In WWW the term format is often used as it is shorter). The content of
  13.    this module is:
  14.    
  15.       Converters
  16.       
  17.       Generic preferences (media type, language, charset etc.)
  18.       
  19.       Global Preferences
  20.       
  21.       Content Negotiation
  22.       
  23.       The Stream Stack
  24.       
  25.    This module is implemented by HTFormat.c, and it is a part of the  W3C Reference
  26.    Library.
  27.    
  28.  */
  29. #ifndef HTFORMAT_H
  30. #define HTFORMAT_H
  31. #include "HTUtils.h"
  32. #include "HTStream.h"
  33. #include "HTAtom.h"
  34. #include "HTList.h"
  35. #include "HTAnchor.h"
  36. #include "HTReq.h"
  37. /*
  38. Stream Converters
  39.    A converter is a stream with a special set of parameters and which is registered as
  40.    capable of converting from a MIME type to something else (maybe another MIME-type). A
  41.    converter is defined to be a function returning a stream and accepting the following
  42.    parameters. The content type elements are atoms for which we have defined a prototype.
  43.    
  44.  */
  45. typedef HTStream * HTConverter  (HTRequest *    request,
  46.                                  void *         param,
  47.                                  HTFormat       input_format,
  48.                                  HTFormat       output_format,
  49.                                  HTStream *     output_stream);
  50. /*
  51. Generic Preferences
  52.    The Library contains functionality for letting the application (or user) express the
  53.    preferences for the rendition of a given data object when issuing a request. The
  54.    categories supported are:
  55.    
  56.       Content type (media type)
  57.       
  58.       Encoding
  59.       
  60.       Language
  61.       
  62.       Charset
  63.       
  64.   REGISTRATION OF ACCEPTED CONTENT TYPES
  65.   
  66.    A presenter is a module (possibly an external program) which can present a graphic
  67.    object of a certain MIME type to the user. That is, presenters are normally used to
  68.    present objects that the converters are not able to handle. Data is transferred to the
  69.    external program using for example the HTSaveAndExecute stream which writes to a local
  70.    file. Both presenters and converters are of the type HTConverter.
  71.    
  72.  */
  73. typedef struct _HTPresentation {
  74.     HTFormat    rep;                         /* representation name atomized */
  75.     HTFormat    rep_out;                         /* resulting representation */
  76.     HTConverter *converter;           /* The routine to gen the stream stack */
  77.     char *      command;                               /* MIME-format string */
  78.     char *      test_command;                          /* MIME-format string */
  79.     double      quality;                     /* Between 0 (bad) and 1 (good) */
  80.     double      secs;
  81.     double      secs_per_byte;
  82. } HTPresentation;
  83. /*
  84.     Predefined Content Types
  85.     
  86.    These macros (which used to be constants) define some basic internally referenced
  87.    representations. The www/xxx ones are of course not MIME standard. They are internal
  88.    representations used in the Library but they can't be exported to other apps!
  89.    
  90.  */
  91. #define WWW_RAW         HTAtom_for("www/void")   /* Raw output from Protocol */
  92. /*
  93.    WWW_RAW is an output format which leaves the input untouched exactly as it is received
  94.    by the protocol module. For example, in the case of FTP, this format returns raw ASCII
  95.    objects for directory listings; for HTTP, everything including the header is returned,
  96.    for Gopher, a raw ASCII object is returned for a menu etc.
  97.    
  98.  */
  99. #define WWW_SOURCE      HTAtom_for("*/*")   /* Almost what it was originally */
  100. /*
  101.    WWW_SOURCE is an output format which leaves the input untouched exactly as it is
  102.    received by the protocol module IF not a suitable converter has been registered with a
  103.    quality factor higher than 1 (for example 2). In this case the SUPER CONVERTER is
  104.    preferred for the raw output. This can be used as a filter effect that allows
  105.    conversion from, for example raw FTPdirectory listings into HTML but passes a MIME body
  106.    untouched.
  107.    
  108.  */
  109. #define WWW_PRESENT     HTAtom_for("www/present")   /* The user's perception */
  110. /*
  111.    WWW_PRESENT represents the user's perception of the document.  If you convert to
  112.    WWW_PRESENT, you present the material to the user.
  113.    
  114.  */
  115. #define WWW_DEBUG       HTAtom_for("www/debug")
  116. /*
  117.    WWW_DEBUG represents the user's perception of debug information, for example sent as a
  118.    HTML document in a HTTP redirection message.
  119.    
  120.  */
  121. #define WWW_UNKNOWN     HTAtom_for("www/unknown")
  122. /*
  123.    WWW_UNKNOWN is a really unknown type. It differs from the real MIME type
  124.    "application/octet-stream" in that we haven't even tried to figure out the content type
  125.    at this point.
  126.    
  127.    These are regular MIME types defined. Others can be added!
  128.    
  129.  */
  130. #define WWW_HTML        HTAtom_for("text/html")
  131. #define WWW_PLAINTEXT   HTAtom_for("text/plain")
  132. #define WWW_MIME        HTAtom_for("message/rfc822")
  133. #define WWW_MIME_HEAD   HTAtom_for("message/x-rfc822-head")
  134. #define WWW_AUDIO       HTAtom_for("audio/basic")
  135. #define WWW_VIDEO       HTAtom_for("video/mpeg")
  136. #define WWW_GIF         HTAtom_for("image/gif")
  137. #define WWW_PNG         HTAtom_for("image/png")
  138. #define WWW_BINARY      HTAtom_for("application/octet-stream")
  139. #define WWW_POSTSCRIPT  HTAtom_for("application/postscript")
  140. #define WWW_RICHTEXT    HTAtom_for("application/rtf")
  141. /*
  142.    We also have some MIME types that come from the various protocols when we convert from
  143.    ASCII to HTML.
  144.    
  145.  */
  146. #define WWW_GOPHER_MENU HTAtom_for("text/x-gopher")
  147. #define WWW_CSO_SEARCH  HTAtom_for("text/x-cso")
  148. #define WWW_FTP_LNST    HTAtom_for("text/x-ftp-lnst")
  149. #define WWW_FTP_LIST    HTAtom_for("text/x-ftp-list")
  150. #define WWW_NNTP_LIST   HTAtom_for("text/x-nntp-list")
  151. #define WWW_NNTP_OVER   HTAtom_for("text/x-nntp-over")
  152. #define WWW_NNTP_HEAD   HTAtom_for("text/x-nntp-head")
  153. #define WWW_HTTP        HTAtom_for("text/x-http")
  154. /*
  155.    Finally we have defined a special format for our RULE files as they can be handled by a
  156.    special converter.
  157.    
  158.  */
  159. #define WWW_RULES       HTAtom_for("application/x-www-rules")
  160. /*
  161.     Add a Presenter
  162.     
  163.    This function creates a presenter object and adds to the list of conversions.
  164.    
  165.   conversions            The list of conveters and presenters
  166.                          
  167.   rep_in                 the MIME-style format name
  168.                          
  169.   rep_out                is the resulting content-type after the conversion
  170.                          
  171.   converter              is the routine to call which actually does the conversion
  172.                          
  173.   quality                A degradation faction [0..1]
  174.                          
  175.   maxbytes               A limit on the length acceptable as input (0 infinite)
  176.                          
  177.   maxsecs                A limit on the time user will wait (0 for infinity)
  178.                          
  179.  */
  180. extern void HTPresentation_add (HTList *        conversions,
  181.                                 CONST char *    representation,
  182.                                 CONST char *    command,
  183.                                 CONST char *    test_command,
  184.                                 double          quality,
  185.                                 double          secs,
  186.                                 double          secs_per_byte);
  187. /*
  188.     Delete a list of Presenters
  189.     
  190.  */
  191. extern void HTPresentation_deleteAll    (HTList * list);
  192. /*
  193.     Add a Converter
  194.     
  195.    This function creates a presenter object and adds to the list of conversions.
  196.    
  197.   conversions            The list of conveters and presenters
  198.                          
  199.   rep_in                 the MIME-style format name
  200.                          
  201.   rep_out                is the resulting content-type after the conversion
  202.                          
  203.   converter              is the routine to call which actually does the conversion
  204.                          
  205.   quality                A degradation faction [0..1]
  206.                          
  207.   maxbytes               A limit on the length acceptable as input (0 infinite)
  208.                          
  209.   maxsecs                A limit on the time user will wait (0 for infinity)
  210.                          
  211.  */
  212. extern void HTConversion_add   (HTList *        conversions,
  213.                                 CONST char *    rep_in,
  214.                                 CONST char *    rep_out,
  215.                                 HTConverter *   converter,
  216.                                 double          quality,
  217.                                 double          secs,
  218.                                 double          secs_per_byte);
  219. /*
  220.     Delete a list of Converters
  221.     
  222.  */
  223. extern void HTConversion_deleteAll      (HTList * list);
  224. /*
  225.   REGISTRATION OF ACCEPTED CONTENT ENCODINGS
  226.   
  227.    Encodings are the HTTP extension of transfer encodings. Encodings include compress,
  228.    gzip etc.
  229.    
  230.  */
  231. typedef struct _HTAcceptNode {
  232.     HTAtom *    atom;
  233.     double      quality;
  234. } HTAcceptNode;
  235. /*
  236.     Predefined Encoding Types
  237.     
  238.  */
  239. #define WWW_ENC_7BIT            HTAtom_for("7bit")
  240. #define WWW_ENC_8BIT            HTAtom_for("8bit")
  241. #define WWW_ENC_BINARY          HTAtom_for("binary")
  242. #define WWW_ENC_BASE64          HTAtom_for("base64")
  243. #define WWW_ENC_COMPRESS        HTAtom_for("compress")
  244. #define WWW_ENC_GZIP            HTAtom_for("gzip")
  245. /*
  246.     Register an Encoding
  247.     
  248.  */
  249. extern void HTEncoding_add (HTList *            list,
  250.                             CONST char *        enc,
  251.                             double              quality);
  252. /*
  253.     Delete a list of Encoders
  254.     
  255.  */
  256. extern void HTEncoding_deleteAll (HTList * list);
  257. /*
  258.   ACCEPTED CHARSETS
  259.   
  260.     Register a Charset
  261.     
  262.  */
  263. extern void HTCharset_add (HTList *             list,
  264.                            CONST char *         charset,
  265.                            double               quality);
  266. /*
  267.     Delete a list of Charsets
  268.     
  269.  */
  270. extern void HTCharset_deleteAll (HTList * list);
  271. /*
  272.   ACCEPTED CONTENT LANGUAGES
  273.   
  274.     Register a Language
  275.     
  276.  */
  277. extern void HTLanguage_add (HTList *            list,
  278.                             CONST char *        lang,
  279.                             double              quality);
  280. /*
  281.     Delete a list of Languages
  282.     
  283.  */
  284. extern void HTLanguage_deleteAll (HTList * list);
  285. /*
  286. Global Registrations
  287.    There are two places where these preferences can be registered: in a global list valid
  288.    for all requests and a local list valid for a particular request only. These are valid
  289.    for all requests. See the Request Manager fro local sets.
  290.    
  291.   CONVERTERS AND PRESENTERS
  292.   
  293.    The global list of specific conversions which the format manager can do in order to
  294.    fulfill the request.  There is also a local list of conversions which contains a
  295.    generic set of possible conversions.
  296.    
  297.  */
  298. extern void HTFormat_setConversion      (HTList *list);
  299. extern HTList * HTFormat_conversion     (void);
  300. /*
  301.   CONTENT ENCODINGS
  302.   
  303.  */
  304. extern void HTFormat_setEncoding        (HTList *list);
  305. extern HTList * HTFormat_encoding       (void);
  306. /*
  307.   CONTENT LANGUAGES
  308.   
  309.  */
  310. extern void HTFormat_setLanguage        (HTList *list);
  311. extern HTList * HTFormat_language       (void);
  312. /*
  313.   CONTENT CHARSETS
  314.   
  315.  */
  316. extern void HTFormat_setCharset         (HTList *list);
  317. extern HTList * HTFormat_charset        (void);
  318. /*
  319.   DELETE ALL GLOBAL LISTS
  320.   
  321.    This is a convenience function that might make life easier.
  322.    
  323.  */
  324. extern void HTFormat_deleteAll (void);
  325. /*
  326. Ranking of Accepted Formats
  327.    This function is used when the best match among several possible documents is to be
  328.    found as a function of the accept headers sent in the client request.
  329.    
  330.  */
  331. typedef struct _HTContentDescription {
  332.     char *      filename;
  333.     HTAtom *    content_type;
  334.     HTAtom *    content_language;
  335.     HTAtom *    content_encoding;
  336.     int         content_length;
  337.     double      quality;
  338. } HTContentDescription;
  339. extern BOOL HTRank (HTList * possibilities,
  340.                     HTList * accepted_content_types,
  341.                     HTList * accepted_content_languages,
  342.                     HTList * accepted_content_encodings);
  343. /*
  344. The Stream Stack
  345.    This is the routine which actually sets up the conversion. It currently checks only for
  346.    direct conversions, but multi-stage conversions are forseen.  It takes a stream into
  347.    which the output should be sent in the final format, builds the conversion stack, and
  348.    returns a stream into which the data in the input format should be fed. If guess is
  349.    true and input format is www/unknown, try to guess the format by looking at the first
  350.    few bytes of the stream.
  351.    
  352.  */
  353. extern HTStream * HTStreamStack (HTFormat       rep_in,
  354.                                  HTFormat       rep_out,
  355.                                  HTStream *     output_stream,
  356.                                  HTRequest *    request,
  357.                                  BOOL           guess);
  358. /*
  359. Cost of a Stream Stack
  360.    Must return the cost of the same stack which HTStreamStack would set up.
  361.    
  362.  */
  363. extern double HTStackValue      (HTList *       conversions,
  364.                                  HTFormat       format_in,
  365.                                  HTFormat       format_out,
  366.                                  double         initial_value,
  367.                                  long int       length);
  368. #endif /* HTFORMAT */
  369. /*
  370.    End of declaration module  */