Annotation of libwww/Library/src/HTFile.c, revision 1.136

1.72      frystyk     1: /*                                                                    HTFile.c
                      2: **     FILE ACCESS
                      3: **
1.79      frystyk     4: **     (c) COPYRIGHT MIT 1995.
1.72      frystyk     5: **     Please first read the full copyright statement in the file COPYRIGH.
1.136   ! eric        6: **     @(#) $Id: HTFile.c,v 1.135 1996/12/03 05:46:01 eric Exp $
1.1       timbl       7: **
                      8: **     This is unix-specific code in general, with some VMS bits.
1.8       timbl       9: **     These are routines for file access used by browsers.
1.1       timbl      10: **
                     11: ** History:
                     12: **        Feb 91       Written Tim Berners-Lee CERN/CN
                     13: **        Apr 91       vms-vms access included using DECnet syntax
                     14: **     26 Jun 92 (JFG) When running over DECnet, suppressed FTP.
                     15: **                     Fixed access bug for relative names on VMS.
1.28      duns       16: **        Sep 93 (MD)  Access to VMS files allows sharing.
                     17: **     15 Nov 93 (MD)  Moved HTVMSname to HTVMSUTILS.C
1.52      duns       18: **     22 Feb 94 (MD)  Excluded two routines if we are not READING directories
1.61      frystyk    19: **     18 May 94 (HF)  Directory stuff removed and stream handling updated,
                     20: **                     error messages introduced etc.
1.120     frystyk    21: **               HFN   Separated transport
1.1       timbl      22: **
                     23: ** Bugs:
1.2       timbl      24: **     FTP: Cannot access VMS files from a unix machine.
                     25: **      How can we know that the
1.1       timbl      26: **     target machine runs VMS?
                     27: */
                     28: 
1.67      frystyk    29: /* Library Includes */
1.112     frystyk    30: #include "sysdep.h"
1.120     frystyk    31: #include "WWWUtil.h"
                     32: #include "WWWCore.h"
1.124     frystyk    33: #include "WWWDir.h"
                     34: #include "WWWTrans.h"
1.126     frystyk    35: #include "HTNetMan.h"
1.120     frystyk    36: #include "HTReqMan.h"
1.124     frystyk    37: #include "HTMulti.h"
1.67      frystyk    38: #include "HTFile.h"            /* Implemented here */
                     39: 
1.91      frystyk    40: /* Final states have negative value */
1.80      frystyk    41: typedef enum _FileState {
1.95      frystyk    42:     FS_RETRY           = -4,
                     43:     FS_ERROR           = -3,
                     44:     FS_NO_DATA         = -2,
                     45:     FS_GOT_DATA                = -1,
                     46:     FS_BEGIN           = 0,
                     47:     FS_DO_CN,
                     48:     FS_NEED_OPEN_FILE,
                     49:     FS_NEED_BODY,
                     50:     FS_PARSE_DIR,
                     51:     FS_TRY_FTP
1.80      frystyk    52: } FileState;
                     53: 
1.91      frystyk    54: /* This is the context structure for the this module */
1.77      frystyk    55: typedef struct _file_info {
1.80      frystyk    56:     FileState          state;            /* Current state of the connection */
1.95      frystyk    57:     char *             local;          /* Local representation of file name */
1.125     frystyk    58:     struct stat                stat_info;            /* Contains actual file chosen */
1.134     frystyk    59:     HTNet *            net;
1.77      frystyk    60: } file_info;
1.2       timbl      61: 
1.80      frystyk    62: struct _HTStream {
1.112     frystyk    63:     const HTStreamClass *      isa;
1.80      frystyk    64: };
                     65: 
1.120     frystyk    66: struct _HTInputStream {
                     67:     const HTInputStreamClass * isa;
                     68: };
                     69: 
1.123     frystyk    70: #if 0
1.95      frystyk    71: PRIVATE BOOL           HTTakeBackup = YES;
1.123     frystyk    72: #endif
1.1       timbl      73: 
1.95      frystyk    74: PRIVATE HTDirReadme    dir_readme = HT_DIR_README_TOP;
                     75: PRIVATE HTDirAccess    dir_access = HT_DIR_OK;
                     76: PRIVATE HTDirShow      dir_show = HT_DS_SIZE+HT_DS_DATE+HT_DS_DES+HT_DS_ICON;
                     77: PRIVATE HTDirKey       dir_key = HT_DK_CINS;
1.2       timbl      78: 
1.61      frystyk    79: /* ------------------------------------------------------------------------- */
1.11      timbl      80: 
1.95      frystyk    81: /*     Directory Access
                     82: **     ----------------
                     83: */
                     84: PUBLIC BOOL HTFile_setDirAccess (HTDirAccess mode)
                     85: {
                     86:     dir_access = mode;
                     87:     return YES;
                     88: }
                     89: 
                     90: PUBLIC HTDirAccess HTFile_dirAccess (void)
                     91: {
                     92:     return dir_access;
                     93: }
                     94: 
                     95: /*     Directory Readme
                     96: **     ----------------
                     97: */
                     98: PUBLIC BOOL HTFile_setDirReadme (HTDirReadme mode)
                     99: {
                    100:     dir_readme = mode;
                    101:     return YES;
                    102: }
                    103: 
                    104: PUBLIC HTDirReadme HTFile_dirReadme (void)
                    105: {
                    106:     return dir_readme;
                    107: }
                    108: 
                    109: /*     HTFile_readDir
                    110: **     --------------
                    111: **     Reads the directory "path"
                    112: **     Returns:
                    113: **             HT_ERROR        Error
1.107     frystyk   114: **             HT_FORBIDDEN    Directory reading not allowed
1.95      frystyk   115: **             HT_LOADED       Successfully read the directory
1.1       timbl     116: */
1.95      frystyk   117: PRIVATE int HTFile_readDir (HTRequest * request, file_info *file)
1.1       timbl     118: {
1.112     frystyk   119: #ifdef HAVE_READDIR
                    120:     DIR * dp;
1.95      frystyk   121:     struct stat file_info;
1.124     frystyk   122:     HTParentAnchor * anchor = HTRequest_anchor(request);
                    123:     char *url = HTAnchor_physical(anchor);
1.95      frystyk   124:     char fullname[HT_MAX_PATH+1];
                    125:     char *name;
1.111     eric      126:     if (PROT_TRACE) HTTrace("Reading..... directory\n");
1.95      frystyk   127:     if (dir_access == HT_DIR_FORBID) {
1.100     frystyk   128:        HTRequest_addError(request, ERR_FATAL, NO, HTERR_FORBIDDEN,
1.95      frystyk   129:                   NULL, 0, "HTFile_readDir");
1.107     frystyk   130:        return HT_FORBIDDEN;
1.95      frystyk   131:     }
                    132:     
                    133:     /* Initialize path name for stat() */
                    134:     if (*(name = (url+strlen(url)-1)) != '/') {
                    135:        char *newurl = NULL;
                    136:        StrAllocCopy(newurl, url);
                    137:        StrAllocCat(newurl, "/");
1.110     frystyk   138:        HT_FREE(file->local);
1.123     frystyk   139:        file->local = HTWWWToLocal(newurl, "", HTRequest_userProfile(request));
1.110     frystyk   140:        HT_FREE(newurl);
1.95      frystyk   141:     }
                    142:     strcpy(fullname, file->local);
                    143:     name = fullname+strlen(fullname);           /* Point to end of fullname */
                    144: 
                    145:     /* Check if access is enabled */
                    146:     if (dir_access == HT_DIR_SELECTIVE) {
                    147:        strcpy(name, DEFAULT_DIR_FILE);
                    148:        if (HT_STAT(fullname, &file_info)) {
1.80      frystyk   149:            if (PROT_TRACE)
1.111     eric      150:                HTTrace(
1.95      frystyk   151:                        "Read dir.... `%s\' not found\n", DEFAULT_DIR_FILE);
1.100     frystyk   152:            HTRequest_addError(request, ERR_FATAL, NO, HTERR_FORBIDDEN,
1.95      frystyk   153:                       NULL, 0, "HTFile_readDir");
1.107     frystyk   154:            return HT_FORBIDDEN;
1.1       timbl     155:        }
                    156:     }
                    157: 
1.95      frystyk   158:     if ((dp = opendir(file->local))) {
1.112     frystyk   159:        struct dirent * dirbuf;
1.95      frystyk   160:        HTDir *dir = HTDir_new(request, dir_show, dir_key);
                    161:        char datestr[20];
                    162:        char sizestr[10];
                    163:        HTFileMode mode;
                    164: #ifdef HT_REENTRANT
1.130     frystyk   165:        struct dirent result;                               /* For readdir_r */
1.129     frystyk   166:        while ((dirbuf = (struct dirent *) readdir_r(dp, &result)))
1.95      frystyk   167: #else
                    168:        while ((dirbuf = readdir(dp)))
                    169: #endif /* HT_REENTRANT */
                    170:        {
                    171:            /* Current and parent directories are never shown in list */
1.112     frystyk   172: #ifdef HAVE_DIRENT_INO
1.95      frystyk   173:            if (!dirbuf->d_ino ||
                    174:                !strcmp(dirbuf->d_name, ".") || !strcmp(dirbuf->d_name, ".."))
1.112     frystyk   175: #else
                    176:            if (!strcmp(dirbuf->d_name, ".") || !strcmp(dirbuf->d_name, ".."))
                    177: #endif
1.95      frystyk   178:                continue;
                    179: 
                    180:            /* Make a lstat on the file */
                    181:            strcpy(name, dirbuf->d_name);
                    182:            if (HT_LSTAT(fullname, &file_info)) {
                    183:                if (PROT_TRACE)
1.111     eric      184:                    HTTrace("Read dir.... lstat failed: %s\n",fullname);
1.95      frystyk   185:                continue;
                    186:            }
1.1       timbl     187: 
1.95      frystyk   188:            /* Convert stat info to fit our setup */
1.103     frystyk   189:            if (((mode_t) file_info.st_mode & S_IFMT) == S_IFDIR) {
1.95      frystyk   190: #ifdef VMS
                    191:                char *dot = strstr(name, ".DIR");      /* strip .DIR part... */
                    192:                if (dot) *dot = '\0';
                    193: #endif /* VMS */
                    194:                mode = HT_IS_DIR;
                    195:                if (dir_show & HT_DS_SIZE) strcpy(sizestr, "-");
                    196:            } else {
                    197:                mode = HT_IS_FILE;
                    198:                if (dir_show & HT_DS_SIZE)
                    199:                    HTNumToStr(file_info.st_size, sizestr, 10);
                    200:            }
                    201:            if (dir_show & HT_DS_DATE)
                    202:                HTDateDirStr(&file_info.st_mtime, datestr, 20);
1.1       timbl     203: 
1.95      frystyk   204:            /* Add to the list */
                    205:            if (HTDir_addElement(dir, name, datestr, sizestr, mode) != YES)
                    206:                break;
                    207:        }
                    208:        closedir(dp);
                    209:        HTDir_free(dir);
1.127     frystyk   210:        return HT_LOADED;
                    211:     } else {
1.100     frystyk   212:        HTRequest_addSystemError(request,  ERR_FATAL, errno, NO, "opendir");
1.127     frystyk   213:        return HT_ERROR;
                    214:     }
1.112     frystyk   215: #else
1.114     eric      216:     return HT_ERROR;   /* needed for WWW_MSWINDOWS */
1.112     frystyk   217: #endif /* HAVE_READDIR */
1.1       timbl     218: }
                    219: 
                    220: /*     Determine write access to a file
1.2       timbl     221: **     --------------------------------
1.80      frystyk   222: **     If stat_info is NULL then the function calls stat() on it's own,
                    223: **     otherwise it uses the information found in stat_info
1.2       timbl     224: ** On exit,
                    225: **     return value    YES if file can be accessed and can be written to.
                    226: **
                    227: ** Bugs:
                    228: **     1.      No code for non-unix systems.
                    229: **     2.      Isn't there a quicker way?
1.1       timbl     230: */
1.112     frystyk   231: PRIVATE BOOL HTEditable (const char * filename, struct stat * stat_info)
1.80      frystyk   232: {
1.112     frystyk   233: #ifdef GETGROUPS_T
1.80      frystyk   234:     int i;
                    235:     uid_t myUid;
                    236:     int        ngroups;                        /* The number of groups  */
1.1       timbl     237:     struct stat        fileStatus;
1.80      frystyk   238:     struct stat *fileptr = stat_info ? stat_info : &fileStatus;
1.112     frystyk   239:     GETGROUPS_T groups[NGROUPS];
1.80      frystyk   240:     if (!stat_info) {
1.90      frystyk   241:        if (HT_STAT(filename, &fileStatus))
1.80      frystyk   242:            return NO;                            /* Can't even access file! */
                    243:     }
1.1       timbl     244:     ngroups = getgroups(NGROUPS, groups);      /* Groups to which I belong  */
                    245:     myUid = geteuid();                         /* Get my user identifier */
                    246: 
1.95      frystyk   247:     if (PROT_TRACE) {
1.1       timbl     248:         int i;
1.111     eric      249:        HTTrace(
1.19      timbl     250:            "File mode is 0%o, uid=%d, gid=%d. My uid=%d, %d groups (",
1.80      frystyk   251:            (unsigned int) fileptr->st_mode, (int) fileptr->st_uid,
                    252:            (int) fileptr->st_gid, (int) myUid, ngroups);
1.111     eric      253:        for (i=0; i<ngroups; i++) HTTrace(" %d", (int) groups[i]);
                    254:        HTTrace(")\n");
1.1       timbl     255:     }
                    256:     
1.80      frystyk   257:     if (fileptr->st_mode & 0002)               /* I can write anyway? */
1.1       timbl     258:        return YES;
                    259:        
1.80      frystyk   260:     if ((fileptr->st_mode & 0200)              /* I can write my own file? */
                    261:      && (fileptr->st_uid == myUid))
1.1       timbl     262:        return YES;
                    263: 
1.80      frystyk   264:     if (fileptr->st_mode & 0020)               /* Group I am in can write? */
1.1       timbl     265:     {
                    266:        for (i=0; i<ngroups; i++) {
1.80      frystyk   267:             if (groups[i] == fileptr->st_gid)
1.1       timbl     268:                return YES;
                    269:        }
                    270:     }
1.111     eric      271:     if (PROT_TRACE) HTTrace("\tFile is not editable.\n");
1.1       timbl     272:     return NO;                                 /* If no excuse, can't do */
1.80      frystyk   273: #else
                    274:     /*
                    275:     ** We don't know and can't find out. Can we be sure that when opening
                    276:     ** a file in mode "a" that the file is not modified?
                    277:     */
                    278:     return NO;
1.112     frystyk   279: #endif /* GETGROUPS_T */
1.1       timbl     280: }
1.80      frystyk   281: 
1.91      frystyk   282: /*     FileCleanup
                    283: **     -----------
1.80      frystyk   284: **      This function closes the connection and frees memory.
1.91      frystyk   285: **      Returns YES on OK, else NO
1.80      frystyk   286: */
1.91      frystyk   287: PRIVATE int FileCleanup (HTRequest *req, int status)
1.80      frystyk   288: {
1.126     frystyk   289:     HTNet * net = HTRequest_net(req);
                    290:     file_info * file = (file_info *) HTNet_context(net);
                    291:     HTStream * input = HTRequest_inputStream(req);
1.106     frystyk   292: 
                    293:     /* Free stream with data TO Local file system */
1.107     frystyk   294:     if (HTRequest_isDestination(req))
                    295:        HTRequest_removeDestination(req);
1.126     frystyk   296:     else  if (input) {
1.106     frystyk   297:        if (status == HT_INTERRUPTED)
1.126     frystyk   298:            (*input->isa->abort)(input, NULL);
1.106     frystyk   299:        else
1.126     frystyk   300:            (*input->isa->_free)(input);
                    301:        HTRequest_setInputStream(req, NULL);
1.106     frystyk   302:     }
                    303: 
1.91      frystyk   304:     if (file) {
1.110     frystyk   305:        HT_FREE(file->local);
                    306:        HT_FREE(file);
1.80      frystyk   307:     }
1.128     frystyk   308:     HTNet_delete(net, status);
1.91      frystyk   309:     return YES;
1.80      frystyk   310: }
                    311: 
                    312: 
1.1       timbl     313: /*     Load a document
                    314: **     ---------------
                    315: **
                    316: ** On entry,
1.80      frystyk   317: **     request         This is the request structure
1.1       timbl     318: ** On exit,
1.91      frystyk   319: **     returns         HT_ERROR        Error has occured in call back
                    320: **                     HT_OK           Call back was OK
1.1       timbl     321: */
1.134     frystyk   322: PRIVATE int FileEvent (SOCKET soc, void * pVoid, HTEventType type);
                    323: 
                    324: PUBLIC int HTLoadFile (SOCKET soc, HTRequest * request)
1.1       timbl     325: {
1.134     frystyk   326:     file_info *file;                         /* Specific access information */
                    327:     HTNet * net = HTRequest_net(request);
                    328:     HTParentAnchor * anchor = HTRequest_anchor(request);
                    329: 
                    330:     if (PROT_TRACE) HTTrace("HTLoadFile.. Looking for `%s\'\n",
                    331:                            HTAnchor_physical(anchor));
                    332:     if ((file = (file_info *) HT_CALLOC(1, sizeof(file_info))) == NULL)
                    333:        HT_OUTOFMEM("HTLoadFILE");
                    334:     file->state = FS_BEGIN;
                    335:     file->net = net;
                    336:     HTNet_setContext(net, file);
                    337:     HTNet_setEventCallback(net, FileEvent);
                    338:     HTNet_setEventParam(net, file);  /* callbacks get http* */
                    339: 
                    340:     return FileEvent(soc, file, HTEvent_BEGIN);                /* get it started - ops is ignored */
                    341: }
                    342: 
                    343: PRIVATE int FileEvent (SOCKET soc, void * pVoid, HTEventType type)
                    344: {
                    345:     file_info *file = pVoid;                         /* Specific access information */
1.80      frystyk   346:     int status = HT_ERROR;
1.134     frystyk   347:     HTNet * net = file->net;
                    348:     HTRequest * request = HTNet_request(net);
1.124     frystyk   349:     HTParentAnchor * anchor = HTRequest_anchor(request);
1.45      luotonen  350: 
1.91      frystyk   351:     /*
                    352:     ** Initiate a new file structure and bind to request structure
                    353:     ** This is actually state FILE_BEGIN, but it can't be in the state
                    354:     ** machine as we need the structure first.
                    355:     */
1.134     frystyk   356:     if (type == HTEvent_CLOSE) {                                     /* Interrupted */
1.107     frystyk   357:        HTRequest_addError(request, ERR_FATAL, NO, HTERR_INTERRUPTED,
1.131     frystyk   358:                           NULL, 0, "HTLoadFile");
1.107     frystyk   359:        FileCleanup(request, HT_INTERRUPTED);
1.91      frystyk   360:        return HT_OK;
1.134     frystyk   361:     }
                    362: 
1.36      luotonen  363: 
1.80      frystyk   364:     /* Now jump into the machine. We know the state from the previous run */
                    365:     while (1) {
                    366:        switch (file->state) {
1.95      frystyk   367:          case FS_BEGIN:
1.102     frystyk   368:            if (HTLib_secure()) {
1.80      frystyk   369:                if (PROT_TRACE)
1.111     eric      370:                    HTTrace("LoadFile.... No access to local file system\n");
1.95      frystyk   371:                file->state = FS_TRY_FTP;
1.80      frystyk   372:                break;
                    373:            }
1.123     frystyk   374:            file->local = HTWWWToLocal(HTAnchor_physical(anchor), "",
                    375:                                       HTRequest_userProfile(request));
1.95      frystyk   376:            if (!file->local) {
                    377:                file->state = FS_TRY_FTP;
1.80      frystyk   378:                break;
1.2       timbl     379:            }
1.135     eric      380: 
1.136   ! eric      381:            if ((net->host = HTHost_new("<local access>", 0)) == NULL)
        !           382:                return HT_ERROR;
1.135     eric      383:            if (HTHost_addNet(net->host, net) == HT_PENDING)
1.136   ! eric      384:                if (PROT_TRACE) HTTrace("HTLoadFile.. Pending...\n");
1.135     eric      385:            
1.125     frystyk   386:            file->state = FS_DO_CN;
1.86      frystyk   387:            break;
                    388: 
1.95      frystyk   389:          case FS_DO_CN:
1.80      frystyk   390:            /*
                    391:            ** If we have to do content negotiation then find the object that
                    392:            ** fits best into either what the client has indicated in the
1.86      frystyk   393:            ** accept headers or what the client has registered on its own.
1.80      frystyk   394:            ** The object chosen can in fact be a directory! However, content
1.134     frystyk   395:            ** negotiation only makes sense if we can read the directory!
1.80      frystyk   396:            ** We stat the file in order to find the size and to see it if
                    397:            ** exists.
                    398:            */
1.132     frystyk   399:            if (HTRequest_negotiation(request) &&
                    400:                HTMethod_isSafe(HTRequest_method(request))) {
1.125     frystyk   401:                char * conneg = HTMulti(request, file->local,&file->stat_info);
                    402:                if (conneg) {
                    403:                    HT_FREE(file->local);
                    404:                    file->local = conneg;
                    405:                    HTAnchor_setPhysical(anchor, conneg);
                    406:                    if (PROT_TRACE)
                    407:                        HTTrace("Load File... Found `%s\'\n", conneg);
1.80      frystyk   408:                } else {
1.125     frystyk   409:                    if (PROT_TRACE)
                    410:                        HTTrace("Load File... Not found - even tried content negotiation\n");
1.126     frystyk   411:                    HTRequest_addError(request, ERR_INFO, NO, HTERR_NOT_FOUND,
                    412:                                       NULL, 0, "HTLoadFile");
1.125     frystyk   413:                    file->state = FS_ERROR;
                    414:                    break;
1.80      frystyk   415:                }
1.125     frystyk   416:            } else {
                    417:                if (HT_STAT(file->local, &file->stat_info) == -1) {
                    418:                    if (PROT_TRACE)
                    419:                        HTTrace("Load.File... Not found `%s\'\n", file->local);
                    420:                    HTRequest_addError(request, ERR_FATAL, NO, HTERR_NOT_FOUND,
                    421:                                       NULL, 0, "HTLoadFile");
                    422:                    file->state = FS_ERROR;
                    423:                    break;
1.80      frystyk   424:                }
1.125     frystyk   425:            }
                    426: 
                    427:            /*
                    428:            ** Check to see if the 'localname' is in fact a directory
                    429:            */
                    430:            if (((file->stat_info.st_mode) & S_IFMT) == S_IFDIR) {
                    431:                file->state = FS_PARSE_DIR;
                    432:                break;
                    433:            }
                    434: 
                    435:            /*
1.132     frystyk   436:            ** If empty file then only serve it if it is editable. We also get
                    437:            ** the bindings for the file suffixes in lack of better bindings
1.125     frystyk   438:            */
                    439:            {
                    440:                BOOL editable = HTEditable(file->local, &file->stat_info);
1.133     frystyk   441:                HTBind_getAnchorBindings(anchor);
                    442:                if (editable) HTAnchor_appendAllow(anchor, METHOD_PUT);
1.125     frystyk   443:                if (file->stat_info.st_size)
                    444:                    HTAnchor_setLength(anchor, file->stat_info.st_size);
                    445:                if (!editable && !file->stat_info.st_size) {
                    446:                    HTRequest_addError(request, ERR_FATAL,NO,HTERR_NO_CONTENT,
                    447:                                       NULL, 0, "HTLoadFile");
                    448:                    file->state = FS_NO_DATA;
                    449:                } else
                    450:                    file->state = FS_NEED_OPEN_FILE;
1.2       timbl     451:            }
1.80      frystyk   452:            break;
1.61      frystyk   453: 
1.95      frystyk   454:          case FS_NEED_OPEN_FILE:
1.120     frystyk   455:            status = HTFileOpen(net, file->local, HT_FT_RDONLY);
                    456:            if (status == HT_OK) {
                    457:                /* 
                    458:                ** Create the stream pipe FROM the channel to the application.
                    459:                ** The target for the input stream pipe is set up using the
1.132     frystyk   460:                ** stream stack.
1.120     frystyk   461:                */
1.134     frystyk   462:                net->readStream = HTStreamStack(HTAnchor_format(anchor),
                    463:                                                HTRequest_outputFormat(request),
                    464:                                                HTRequest_outputStream(request),
                    465:                                                request, YES);
1.126     frystyk   466:                HTRequest_setOutputConnected(request, YES);
                    467: 
1.120     frystyk   468:                /*
                    469:                ** Create the stream pipe TO the channel from the application
                    470:                ** and hook it up to the request object
                    471:                */
                    472:                {
                    473:                    HTOutputStream * output = HTNet_getOutput(net, NULL, 0);
                    474:                    HTRequest_setInputStream(request, (HTStream *) output);
                    475:                }
1.80      frystyk   476: 
1.120     frystyk   477:                /*
                    478:                ** Set up concurrent read/write if this request isn't the
                    479:                ** source for a PUT or POST. As source we don't start reading
                    480:                ** before all destinations are ready. If destination then
                    481:                ** register the input stream and get ready for read
                    482:                */
                    483:                if (HTRequest_isDestination(request)) {
1.134     frystyk   484:                    HTEvent_register(HTNet_socket(net), HTEvent_READ, &net->event);
1.120     frystyk   485:                    HTRequest_linkDestination(request);
1.82      frystyk   486:                }
1.120     frystyk   487:                if (HTRequest_isSource(request) &&
                    488:                    !HTRequest_destinationsReady(request))
                    489:                    return HT_OK;
1.126     frystyk   490:                HTRequest_addError(request, ERR_INFO, NO, HTERR_OK,
                    491:                                   NULL, 0, "HTLoadFile");
1.120     frystyk   492:                file->state = FS_NEED_BODY;
1.106     frystyk   493: 
1.116     frystyk   494: #ifndef NO_UNIX_IO
1.120     frystyk   495:                /* If we are _not_ using preemptive mode and we are Unix fd's
                    496:                ** then return here to get the same effect as when we are
                    497:                ** connecting to a socket. That way, HTFile acts just like any
                    498:                ** other protocol module even though we are in fact doing
                    499:                ** blocking connect
                    500:                */
                    501:                if (!net->preemptive) {
                    502:                    if (PROT_TRACE) HTTrace("HTLoadFile.. returning\n");
1.134     frystyk   503:                    HTEvent_register(HTNet_socket(net), HTEvent_READ, &net->event);
1.132     frystyk   504:                    return HT_OK;
1.120     frystyk   505:                }
1.116     frystyk   506: #endif
1.131     frystyk   507:            } else if (status == HT_WOULD_BLOCK || status == HT_PENDING)
1.120     frystyk   508:                return HT_OK;
1.126     frystyk   509:            else {
                    510:                HTRequest_addError(request, ERR_INFO, NO, HTERR_INTERNAL,
                    511:                                   NULL, 0, "HTLoadFile");
1.120     frystyk   512:                file->state = FS_ERROR;                /* Error or interrupt */
1.126     frystyk   513:            }
1.107     frystyk   514:            break;
1.82      frystyk   515: 
1.95      frystyk   516:          case FS_NEED_BODY:
1.134     frystyk   517:            status = HTHost_read(net->host, net);
1.80      frystyk   518:            if (status == HT_WOULD_BLOCK)
1.91      frystyk   519:                return HT_OK;
1.108     frystyk   520:            else if (status == HT_LOADED || status == HT_CLOSED) {
1.95      frystyk   521:                file->state = FS_GOT_DATA;
1.126     frystyk   522:            } else {
                    523:                HTRequest_addError(request, ERR_INFO, NO, HTERR_FORBIDDEN,
                    524:                                   NULL, 0, "HTLoadFile");
1.95      frystyk   525:                file->state = FS_ERROR;
1.126     frystyk   526:            }
1.80      frystyk   527:            break;
1.1       timbl     528: 
1.95      frystyk   529:          case FS_PARSE_DIR:
                    530:            status = HTFile_readDir(request, file);
                    531:            if (status == HT_LOADED)
                    532:                file->state = FS_GOT_DATA;
1.127     frystyk   533:            else
1.95      frystyk   534:                file->state = FS_ERROR;
1.80      frystyk   535:            break;
                    536: 
1.95      frystyk   537:          case FS_TRY_FTP:
1.80      frystyk   538:            {
1.99      frystyk   539:                char *url = HTAnchor_physical(anchor);
1.80      frystyk   540:                HTAnchor *anchor;
                    541:                char *newname = NULL;
                    542:                StrAllocCopy(newname, "ftp:");
                    543:                if (!strncmp(url, "file:", 5))
                    544:                    StrAllocCat(newname, url+5);
                    545:                else
                    546:                    StrAllocCat(newname, url);
                    547:                anchor = HTAnchor_findAddress(newname);
1.109     frystyk   548:                HTRequest_setAnchor(request, anchor);
1.110     frystyk   549:                HT_FREE(newname);
1.91      frystyk   550:                FileCleanup(request, HT_IGNORE);
1.97      frystyk   551:                return HTLoad(request, YES);
1.80      frystyk   552:            }
                    553:            break;
1.1       timbl     554: 
1.95      frystyk   555:          case FS_GOT_DATA:
1.83      frystyk   556:            if (HTRequest_isPostWeb(request)) {
1.87      frystyk   557:                if (HTRequest_isDestination(request)) {
1.124     frystyk   558:                    HTRequest * source = HTRequest_source(request);
1.87      frystyk   559:                    HTLink *link =
1.126     frystyk   560:                        HTLink_find((HTAnchor *)HTRequest_anchor(source),
1.99      frystyk   561:                                          (HTAnchor *) anchor);
1.104     frystyk   562:                    HTLink_setResult(link, HT_LINK_OK);
1.87      frystyk   563:                }
1.107     frystyk   564:            }
                    565:            FileCleanup(request, HT_LOADED);
1.91      frystyk   566:            return HT_OK;
1.80      frystyk   567:            break;
                    568: 
1.95      frystyk   569:          case FS_NO_DATA:
1.83      frystyk   570:            if (HTRequest_isPostWeb(request)) {
1.87      frystyk   571:                if (HTRequest_isDestination(request)) {
1.124     frystyk   572:                    HTRequest * source = HTRequest_source(request);
1.87      frystyk   573:                    HTLink *link =
1.126     frystyk   574:                        HTLink_find((HTAnchor *)HTRequest_anchor(source),
1.99      frystyk   575:                                          (HTAnchor *) anchor);
1.104     frystyk   576:                    HTLink_setResult(link, HT_LINK_OK);
1.87      frystyk   577:                }
1.107     frystyk   578:            }
                    579:            FileCleanup(request, HT_NO_DATA);
1.91      frystyk   580:            return HT_OK;
1.80      frystyk   581:            break;
                    582: 
1.95      frystyk   583:          case FS_RETRY:
1.83      frystyk   584:            if (HTRequest_isPostWeb(request)) {
1.87      frystyk   585:                if (HTRequest_isDestination(request)) {
1.124     frystyk   586:                    HTRequest * source = HTRequest_source(request);
1.87      frystyk   587:                    HTLink *link =
1.126     frystyk   588:                        HTLink_find((HTAnchor *)HTRequest_anchor(source),
1.99      frystyk   589:                                          (HTAnchor *) anchor);
1.104     frystyk   590:                    HTLink_setResult(link, HT_LINK_ERROR);
1.87      frystyk   591:                }
1.107     frystyk   592:                HTRequest_killPostWeb(request);
                    593:            }
                    594:            FileCleanup(request, HT_RETRY);
1.91      frystyk   595:            return HT_OK;
1.80      frystyk   596:            break;
                    597: 
1.95      frystyk   598:          case FS_ERROR:
1.83      frystyk   599:            if (HTRequest_isPostWeb(request)) {
1.87      frystyk   600:                if (HTRequest_isDestination(request)) {
1.124     frystyk   601:                    HTRequest * source = HTRequest_source(request);
1.87      frystyk   602:                    HTLink *link =
1.126     frystyk   603:                        HTLink_find((HTAnchor *)HTRequest_anchor(source),
1.99      frystyk   604:                                          (HTAnchor *) anchor);
1.104     frystyk   605:                    HTLink_setResult(link, HT_LINK_ERROR);
1.87      frystyk   606:                }
1.107     frystyk   607:                HTRequest_killPostWeb(request);
                    608:            }
                    609:            FileCleanup(request, HT_ERROR);
1.91      frystyk   610:            return HT_OK;
1.80      frystyk   611:            break;
                    612:        }
                    613:     } /* End of while(1) */
1.1       timbl     614: }

Webmaster