Annotation of libwww/Library/src/HTFormat.html, revision 2.27

2.10      timbl       1: <HTML>
                      2: <HEAD>
2.1       timbl       3: <TITLE>HTFormat: The format manager in the WWW Library</TITLE>
2.15      timbl       4: <NEXTID N="z18">
2.10      timbl       5: </HEAD>
2.1       timbl       6: <BODY>
                      7: <H1>Manage different document formats</H1>Here we describe the functions of
2.27    ! frystyk     8: the HTFormat module which handles conversion between different data
        !             9: representations.  (In MIME parlance, a representation is known as a content-
        !            10: type. In WWW  the term "format" is often used as it is shorter).<P>
        !            11: This module is implemented by <A NAME="z0" HREF="HTFormat.c">HTFormat.c</A>.
        !            12: 
        !            13: The module is a part of the <A NAME="z10" HREF="Overview.html">WWW library</A>.
        !            14: 
2.1       timbl      15: <H2>Preamble</H2>
                     16: <PRE>#ifndef HTFORMAT_H
                     17: #define HTFORMAT_H
                     18: 
                     19: #include "HTUtils.h"
                     20: #include <A
2.10      timbl      21: NAME="z7" HREF="HTStream.html">"HTStream.h"</A>
2.1       timbl      22: #include "HTAtom.h"
2.2       timbl      23: #include "HTList.h"
2.1       timbl      24: 
                     25: #ifdef SHORT_NAMES
                     26: #define HTOutputSource HTOuSour
                     27: #define HTOutputBinary HTOuBina
                     28: #endif
                     29: 
2.18      luotonen   30: 
                     31: typedef struct _HTContentDescription {
                     32:     char *     filename;
                     33:     HTAtom *   content_type;
                     34:     HTAtom *   content_language;
                     35:     HTAtom *   content_encoding;
                     36:     int                content_length;
                     37:     float      quality;
                     38: } HTContentDescription;
                     39: 
                     40: PUBLIC void HTAcceptEncoding PARAMS((HTList *  list,
                     41:                                     char *     enc,
                     42:                                     float      quality));
                     43: 
                     44: PUBLIC void HTAcceptLanguage PARAMS((HTList *  list,
                     45:                                     char *     lang,
                     46:                                     float      quality));
                     47: 
                     48: PUBLIC BOOL HTRank PARAMS((HTList * possibilities,
                     49:                           HTList * accepted_content_types,
                     50:                           HTList * accepted_content_languages,
                     51:                           HTList * accepted_content_encodings));
                     52: 
                     53: 
2.1       timbl      54: </PRE>
2.17      luotonen   55: <H2>HT<A
                     56: NAME="z15"> Input Socket: Buffering for network
                     57: in</A></H2>This routines provide simple character
                     58: input from sockets. These are used
                     59: for parsing input in arbitrary IP
                     60: protocols (Gopher, NNTP, FTP).
                     61: <PRE>#define INPUT_BUFFER_SIZE 4096            /* Tradeoff spped vs memory*/
                     62: typedef struct _socket_buffer {
2.25      luotonen   63:        char input_buffer[INPUT_BUFFER_SIZE];
2.17      luotonen   64:        char * input_pointer;
                     65:        char * input_limit;
                     66:        int input_file_number;
2.25      luotonen   67:        BOOL    s_do_buffering;
                     68:        char *  s_buffer;
                     69:        int     s_buffer_size;
                     70:        char *  s_buffer_cur;
2.17      luotonen   71: } HTInputSocket;
                     72: 
                     73: </PRE>
                     74: <H3>Create input buffer and set file
                     75: number</H3>
                     76: <PRE>extern HTInputSocket* HTInputSocket_new PARAMS((int file_number));
                     77: 
                     78: </PRE>
                     79: <H3>Get next character from buffer</H3>
2.24      frystyk    80: <PRE>extern int HTInputSocket_getCharacter PARAMS((HTInputSocket* isoc));
2.17      luotonen   81: 
                     82: </PRE>
2.22      timbl      83: <H3>Read block from input socket</H3>Read *len characters and return a
                     84: buffer (don't free) containing *len
                     85: characters ( *len may have changed).
                     86: Buffer is not NULL-terminated.
2.17      luotonen   87: <PRE>extern char * HTInputSocket_getBlock PARAMS((HTInputSocket * isoc,
                     88:                                                  int *           len));
                     89: 
                     90: </PRE>
                     91: <H3>Free input socket buffer</H3>
                     92: <PRE>extern void HTInputSocket_free PARAMS((HTInputSocket * isoc));
                     93: 
2.22      timbl      94: 
2.17      luotonen   95: PUBLIC char * HTInputSocket_getLine PARAMS((HTInputSocket * isoc));
                     96: PUBLIC char * HTInputSocket_getUnfoldedLine PARAMS((HTInputSocket * isoc));
                     97: PUBLIC char * HTInputSocket_getStatusLine PARAMS((HTInputSocket * isoc));
                     98: PUBLIC BOOL   HTInputSocket_seemsBinary PARAMS((HTInputSocket * isoc));
                     99: 
                    100: </PRE>
2.25      luotonen  101: 
                    102: 
                    103: <H3>Security Buffering</H3>
                    104: 
                    105: When it's necessary to get e.g. the header section, or part of it,
                    106: exactly as it came from the client to calculate the message digest,
                    107: these functions turn buffering on and off.  All the material returned
                    108: by <CODE>HTInputSocket_getStatusLine()</CODE>,
                    109: <CODE>HTInputSocket_getUnfoldedLine()</CODE> and
                    110: <CODE>HTInputSocket_getLine()</CODE> gets buffered after a call to
                    111: <CODE>HTInputSocket_startBuffering()</CODE> until either
                    112: <CODE>HTInputSocket_stopBuffering()</CODE> is called, or an empty line is
                    113: returned by any of these functions (end of body section).
                    114: <CODE>HTInputSocket_getBuffer()</CODE> returns the number of
                    115: characters buffered, and sets the given buffer pointer to point to
                    116: internal buffer.  This buffer exists until <CODE>HTInputSocket</CODE>
                    117: object is freed.
                    118: <PRE>
                    119: 
                    120: PUBLIC void HTInputSocket_startBuffering PARAMS((HTInputSocket * isoc));
                    121: PUBLIC void HTInputSocket_stopBuffering PARAMS((HTInputSocket * isoc));
                    122: PUBLIC int HTInputSocket_getBuffer PARAMS((HTInputSocket * isoc,
                    123:                                           char ** buffer_ptr));
                    124: </PRE>
                    125: 
2.1       timbl     126: <H2>The HTFormat type</H2>We use the HTAtom object for holding
                    127: representations. This allows faster
                    128: manipulation (comparison and copying)
2.14      timbl     129: that if we stayed with strings.<P>
                    130: The following have to be defined
                    131: in advance of the other include files
                    132: because of circular references.
2.1       timbl     133: <PRE>typedef HTAtom * HTFormat;
2.13      timbl     134: 
2.14      timbl     135: #include <A
                    136: NAME="z14" HREF="HTAccess.html">"HTAccess.h"</A>   /* Required for HTRequest definition */
                    137:                
2.1       timbl     138: </PRE>These macros (which used to be constants)
                    139: define some basic internally referenced
2.13      timbl     140: representations. 
                    141: <H3>Internal ones</H3>The www/xxx ones are of course not
                    142: MIME standard.<P>
2.20      frystyk   143: star/star  is an output format which
2.1       timbl     144: leaves the input untouched. It is
                    145: useful for diagnostics, and for users
                    146: who want to see the original, whatever
                    147: it is.
2.13      timbl     148: <H3></H3>
2.20      frystyk   149: <PRE>#define WWW_SOURCE HTAtom_for("*/*")      /* Whatever it was originally*/
2.1       timbl     150: 
                    151: </PRE>www/present represents the user's
                    152: perception of the document.  If you
                    153: convert to www/present, you present
                    154: the material to the user. 
                    155: <PRE>#define WWW_PRESENT HTAtom_for("www/present")     /* The user's perception */
                    156: 
                    157: </PRE>The message/rfc822 format means a
                    158: MIME message or a plain text message
                    159: with no MIME header. This is what
                    160: is returned by an HTTP server.
                    161: <PRE>#define WWW_MIME HTAtom_for("www/mime")           /* A MIME message */
2.10      timbl     162: 
2.1       timbl     163: </PRE>www/print is like www/present except
                    164: it represents a printed copy.
                    165: <PRE>#define WWW_PRINT HTAtom_for("www/print") /* A printed copy */
                    166: 
2.10      timbl     167: </PRE>www/unknown is a really unknown type.
2.11      timbl     168: Some default action is appropriate.
2.10      timbl     169: <PRE>#define WWW_UNKNOWN     HTAtom_for("www/unknown")
                    170: 
2.13      timbl     171: 
                    172: 
                    173: </PRE>
                    174: <H3>MIME ones (a few)</H3>These are regular MIME types.  HTML
2.11      timbl     175: is assumed to be added by the W3
                    176: code. application/octet-stream was
                    177: mistakenly application/binary in
                    178: earlier libwww versions (pre 2.11).
2.10      timbl     179: <PRE>#define WWW_PLAINTEXT     HTAtom_for("text/plain")
2.1       timbl     180: #define WWW_POSTSCRIPT         HTAtom_for("application/postscript")
                    181: #define WWW_RICHTEXT   HTAtom_for("application/rtf")
2.10      timbl     182: #define WWW_AUDIO       HTAtom_for("audio/basic")
2.1       timbl     183: #define WWW_HTML       HTAtom_for("text/html")
2.11      timbl     184: #define WWW_BINARY     HTAtom_for("application/octet-stream")
2.26      frystyk   185: #define WWW_VIDEO      HTAtom_for("video/mpeg")
2.7       timbl     186: 
2.1       timbl     187: </PRE>We must include the following file
                    188: after defining HTFormat, to which
2.10      timbl     189: it makes reference.
                    190: <H2>The HTEncoding type</H2>
                    191: <PRE>typedef HTAtom* HTEncoding;
                    192: 
                    193: </PRE>The following are values for the
                    194: MIME types:
                    195: <PRE>#define WWW_ENC_7BIT              HTAtom_for("7bit")
                    196: #define WWW_ENC_8BIT           HTAtom_for("8bit")
                    197: #define WWW_ENC_BINARY         HTAtom_for("binary")
                    198: 
                    199: </PRE>We also add
                    200: <PRE>#define WWW_ENC_COMPRESS  HTAtom_for("compress")
                    201: 
                    202: #include "HTAnchor.h"
2.1       timbl     203: 
                    204: </PRE>
                    205: <H2>The HTPresentation and HTConverter
                    206: types</H2>This HTPresentation structure represents
                    207: a possible conversion algorithm from
                    208: one format to annother.  It includes
                    209: a pointer to a conversion routine.
                    210: The conversion routine returns a
                    211: stream to which data should be fed.
                    212: See also <A
2.12      timbl     213: NAME="z5" HREF="#z3">HTStreamStack</A> which scans
2.1       timbl     214: the list of registered converters
                    215: and calls one. See the <A
2.10      timbl     216: NAME="z6" HREF="HTInit.html">initialisation
2.1       timbl     217: module</A> for a list of conversion routines.
                    218: <PRE>typedef struct _HTPresentation HTPresentation;
                    219: 
2.13      timbl     220: typedef HTStream * <A
                    221: NAME="z12">HTConverter</A> PARAMS((
                    222:        HTRequest *             request,
                    223:        void *                  param,
                    224:        HTFormat                input_format,
                    225:        HTFormat                output_format,
                    226:        HTStream *              output_stream));
2.1       timbl     227:        
                    228: struct _HTPresentation {
2.13      timbl     229:        HTAtom* rep;            /* representation name atomized */
2.1       timbl     230:        HTAtom* rep_out;        /* resulting representation */
2.2       timbl     231:        HTConverter *converter; /* The routine to gen the stream stack */
2.1       timbl     232:        char *  command;        /* MIME-format string */
                    233:        float   quality;        /* Between 0 (bad) and 1 (good) */
                    234:        float   secs;
                    235:        float   secs_per_byte;
                    236: };
                    237: 
2.15      timbl     238: </PRE>A global list of converters is kept
2.1       timbl     239: by this module.  It is also scanned
                    240: by modules which want to know the
                    241: set of formats supported. for example.
2.22      timbl     242: Note there is also an additional
2.15      timbl     243: list associated with each <A
2.22      timbl     244: NAME="z16" HREF="HTAccess.html#z5">request</A>
                    245: .
2.15      timbl     246: <PRE>extern HTList * <A
                    247: NAME="z17">HTConversions</A> ;
2.1       timbl     248: 
2.12      timbl     249: 
2.1       timbl     250: </PRE>
                    251: <H2>HTSetPresentation: Register a system
                    252: command to present a format</H2>
2.8       timbl     253: <H3>On entry,</H3>
2.1       timbl     254: <DL>
                    255: <DT>rep
                    256: <DD> is the MIME - style format name
                    257: <DT>command
                    258: <DD> is the MAILCAP - style command
                    259: template
                    260: <DT>quality
                    261: <DD> A degradation faction 0..1
                    262: <DT>maxbytes
                    263: <DD> A limit on the length acceptable
                    264: as input (0 infinite)
                    265: <DT>maxsecs
                    266: <DD> A limit on the time user
                    267: will wait (0 for infinity)
                    268: </DL>
                    269: 
                    270: <PRE>extern void HTSetPresentation PARAMS((
2.13      timbl     271:        HTList *        conversions,
                    272:        CONST char *    representation,
                    273:        CONST char *    command,
                    274:        float           quality,
                    275:        float           secs, 
                    276:        float           secs_per_byte
2.1       timbl     277: ));
                    278: 
                    279: 
                    280: </PRE>
                    281: <H2>HTSetConversion:   Register a converstion
                    282: routine</H2>
2.8       timbl     283: <H3>On entry,</H3>
2.1       timbl     284: <DL>
                    285: <DT>rep_in
                    286: <DD> is the content-type input
                    287: <DT>rep_out
                    288: <DD> is the resulting content-type
                    289: <DT>converter
                    290: <DD> is the routine to make
                    291: the stream to do it
                    292: </DL>
                    293: 
                    294: <PRE>
                    295: extern void HTSetConversion PARAMS((
2.13      timbl     296:        HTList *        conversions,
2.1       timbl     297:        CONST char *    rep_in,
                    298:        CONST char *    rep_out,
2.2       timbl     299:        HTConverter *   converter,
2.1       timbl     300:        float           quality,
                    301:        float           secs, 
                    302:        float           secs_per_byte
                    303: ));
                    304: 
                    305: 
                    306: </PRE>
                    307: <H2><A
2.10      timbl     308: NAME="z3">HTStreamStack:   Create a stack of
2.1       timbl     309: streams</A></H2>This is the routine which actually
                    310: sets up the conversion. It currently
                    311: checks only for direct conversions,
2.8       timbl     312: but multi-stage conversions are forseen.
2.2       timbl     313: It takes a stream into which the
2.1       timbl     314: output should be sent in the final
                    315: format, builds the conversion stack,
                    316: and returns a stream into which the
                    317: data in the input format should be
                    318: fed.  The anchor is passed because
                    319: hypertxet objects load information
                    320: into the anchor object which represents
2.23      luotonen  321: them. <P>
                    322: If <CODE>guess</CODE> is true and input format is
                    323: <CODE>www/unknown</CODE>, try to guess the format
                    324: by looking at the first few butes of the stream. <P>
2.1       timbl     325: <PRE>extern HTStream * HTStreamStack PARAMS((
                    326:        HTFormat                format_in,
2.23      luotonen  327:        HTRequest *             request,
                    328:        BOOL                    guess));
2.1       timbl     329: 
                    330: </PRE>
                    331: <H2>HTStackValue: Find the cost of a
                    332: filter stack</H2>Must return the cost of the same
                    333: stack which HTStreamStack would set
                    334: up.
2.8       timbl     335: <H3>On entry,</H3>
2.1       timbl     336: <DL>
                    337: <DT>format_in
                    338: <DD> The fomat of the data to
                    339: be converted
                    340: <DT>format_out
                    341: <DD> The format required
                    342: <DT>initial_value
                    343: <DD> The intrinsic "value"
                    344: of the data before conversion on
                    345: a scale from 0 to 1
                    346: <DT>length
                    347: <DD> The number of bytes expected
                    348: in the input format
                    349: </DL>
                    350: 
                    351: <PRE>extern float HTStackValue PARAMS((
2.13      timbl     352:        HTList *                conversions,
2.1       timbl     353:        HTFormat                format_in,
2.13      timbl     354:        HTFormat                format_out,
2.1       timbl     355:        float                   initial_value,
                    356:        long int                length));
                    357: 
                    358: #define NO_VALUE_FOUND -1e20           /* returned if none found */
                    359: 
                    360: </PRE>
                    361: <H2><A
2.10      timbl     362: NAME="z1">HTCopy:  Copy a socket to a stream</A></H2>This is used by the protocol engines
2.6       secret    363: to send data down a stream, typically
2.22      timbl     364: one which has been generated by HTStreamStack.
                    365: Returns the number of bytes transferred.
2.19      luotonen  366: <PRE>extern int HTCopy PARAMS((
2.1       timbl     367:        int                     file_number,
                    368:        HTStream*               sink));
                    369: 
                    370:        
2.6       secret    371: </PRE>
                    372: <H2><A
2.10      timbl     373: NAME="c6">HTFileCopy:  Copy a file to a stream</A></H2>This is used by the protocol engines
2.6       secret    374: to send data down a stream, typically
2.7       timbl     375: one which has been generated by HTStreamStack.
                    376: It is currently called by <A
2.12      timbl     377: NAME="z9" HREF="#c7">HTParseFile</A>
2.6       secret    378: <PRE>extern void HTFileCopy PARAMS((
                    379:        FILE*                   fp,
                    380:        HTStream*               sink));
                    381: 
                    382:        
2.7       timbl     383: </PRE>
                    384: <H2><A
2.10      timbl     385: NAME="c2">HTCopyNoCR: Copy a socket to a stream,
2.7       timbl     386: stripping CR characters.</A></H2>It is slower than <A
2.12      timbl     387: NAME="z2" HREF="#z1">HTCopy</A> .
2.1       timbl     388: <PRE>
                    389: extern void HTCopyNoCR PARAMS((
                    390:        int                     file_number,
                    391:        HTStream*               sink));
                    392: 
2.16      luotonen  393: 
                    394: </PRE>
2.1       timbl     395: <H2>HTParseSocket: Parse a socket given
                    396: its format</H2>This routine is called by protocol
                    397: modules to load an object.  uses<A
2.12      timbl     398: NAME="z4" HREF="#z3">
2.1       timbl     399: HTStreamStack</A> and the copy routines
                    400: above.  Returns HT_LOADED if succesful,
                    401: &lt;0 if not.
                    402: <PRE>extern int HTParseSocket PARAMS((
                    403:        HTFormat        format_in,
                    404:        int             file_number,
2.13      timbl     405:        HTRequest *     request));
2.6       secret    406: 
                    407: </PRE>
                    408: <H2><A
2.10      timbl     409: NAME="c1">HTParseFile: Parse a File through
2.7       timbl     410: a file pointer</A></H2>This routine is called by protocols
                    411: modules to load an object. uses<A
2.12      timbl     412: NAME="z4" HREF="#z3"> HTStreamStack</A>
2.7       timbl     413: and <A
2.12      timbl     414: NAME="c7" HREF="#c6">HTFileCopy</A> .  Returns HT_LOADED
2.7       timbl     415: if succesful, &lt;0 if not.
2.6       secret    416: <PRE>extern int HTParseFile PARAMS((
                    417:        HTFormat        format_in,
                    418:        FILE            *fp,
2.13      timbl     419:        HTRequest *     request));
2.8       timbl     420: 
                    421: </PRE>
2.11      timbl     422: <H2><A
                    423: NAME="z11">HTNetToText: Convert Net ASCII to
                    424: local representation</A></H2>This is a filter stream suitable
                    425: for taking text from a socket and
                    426: passing it into a stream which expects
                    427: text in the local C representation.
                    428: It does ASCII and newline conversion.
                    429: As usual, pass its output stream
                    430: to it when creating it.
                    431: <PRE>extern HTStream *  HTNetToText PARAMS ((HTStream * sink));
                    432: 
                    433: </PRE>
2.8       timbl     434: <H2>HTFormatInit: Set up default presentations
                    435: and conversions</H2>These are defined in HTInit.c or
                    436: HTSInit.c if these have been replaced.
                    437: If you don't call this routine, and
                    438: you don't define any presentations,
                    439: then this routine will automatically
                    440: be called the first time a conversion
                    441: is needed. However, if you explicitly
                    442: add some conversions (eg using HTLoadRules)
                    443: then you may want also to explicitly
                    444: call this to get the defaults as
                    445: well.
2.20      frystyk   446: <PRE>
                    447: extern void HTFormatInit PARAMS((HTList * conversions));
2.22      timbl     448: 
2.21      frystyk   449: </PRE>
2.22      timbl     450: <H2>HTFormatInitNIM: Set up default presentations
                    451: and conversions</H2>This is a slightly different version
                    452: of HTFormatInit, but without any
                    453: conversions that might use third
                    454: party programs. This is intended
                    455: for Non Interactive Mode.
                    456: <PRE>extern void HTFormatInitNIM PARAMS((HTList * conversions));
2.21      frystyk   457: 
2.1       timbl     458: </PRE>
2.22      timbl     459: <H2>HTFormatDelete: Remove presentations
                    460: and conversions</H2>Deletes the list from HTFormatInit
                    461: or HTFormatInitNIM
                    462: <PRE>extern void HTFormatDelete PARAMS((HTList * conversions));
2.21      frystyk   463: 
                    464: </PRE>
                    465: 
2.1       timbl     466: <H2>Epilogue</H2>
                    467: <PRE>extern BOOL HTOutputSource;       /* Flag: shortcut parser */
                    468: #endif
                    469: 
2.22      timbl     470: </PRE>end</BODY>
2.10      timbl     471: </HTML>

Webmaster