Beispiel #1
0
/*
**   This function tries really hard to find a non-existent filename relative
**   to the path given. Returns a string that must be freed by the caller or
**   NULL on error.
*/
PRIVATE char * get_filename (char * base, const char * uri,
			     const char * suffix, BOOL use_last_segment)
{
    char * path = NULL;
    if (use_last_segment) {
	char * uri_path = NULL;
	if (uri && (uri_path = HTParse(uri, "", PARSE_PATH|PARSE_PUNCTUATION))) {
	    char * last_segment = strrchr(uri_path, '/');
	    BOOL slash = (base && *(base+strlen(base)-1)==DIR_SEPARATOR_CHAR); 
	    if (last_segment && *(last_segment+1)) {
		StrAllocMCopy(&path, base ? base : "",
			      slash ? "" : DIR_SEPARATOR_STR, ++last_segment, NULL);
	    } else {
		StrAllocMCopy(&path, base ? base : "",
			      slash ? "" : DIR_SEPARATOR_STR, DEFAULT_LAST_SEGMENT,
			      suffix ? suffix : "", NULL);
	    }
	}
    } else {
	path = HTGetTmpFileName(base);
        if (path && suffix) StrAllocCat(path, suffix);
    }

    HTTRACE(STREAM_TRACE, "Save file... Temporaray file `%s\'\n" _ path ? path : "<null>");
    return path;
}
Beispiel #2
0
/*	Convert a local file name into a URL
**	------------------------------------
**	Generates a WWW URL name from a local file name or NULL if error.
**	Returns:
**		OK:	local file (that must be freed by caller)
**		Error:	NULL
*/
PUBLIC char * HTLocalToWWW (const char * local, const char * access)
{
    char * escaped = NULL;
    const char * scheme = (access && *access) ? access : "file:";
    if (local && *local) {
#ifdef VMS
        char * unescaped = NULL;
        if ((unescaped = (char *) HT_MALLOC(strlen(local) + 10)) == NULL)
            HT_OUTOFMEM("HTLocalToWWW");
        strcpy(unescaped, scheme);	     /* We get an absolute file name */

        /* convert directory name to Unix-style syntax */
        {
            char * disk = strchr (local, ':');
            char * dir = strchr (local, '[');
            if (disk) {
                *disk = '\0';
                strcat(unescaped, "/");
                strcat(unescaped, local);
            }
            if (dir) {
                char *p;
                *dir = '/';	/* Convert leading '[' */
                for (p = dir ; *p != ']'; ++p)
                    if (*p == '.') *p = '/';
                *p = '\0';	/* Cut on final ']' */
                strcat(unescaped, dir);
            }
        }
        escaped = HTEscape(unescaped, URL_DOSFILE);
        HT_FREE(unescaped);

#else  /* not VMS */
#ifdef WIN32
        char * unescaped = NULL;
        if ((unescaped = (char *) HT_MALLOC(strlen(local) + 10)) == NULL)
            HT_OUTOFMEM("HTLocalToWWW");
        strcpy(unescaped, scheme);	     /* We get an absolute file name */
        if (strchr(local, ':')) strcat(unescaped, "/");
        {
            const char *p = local;
            char *q = unescaped+strlen(unescaped);
            while (*p) {
                if (*p=='\\') {
                    *q++='/';
                } else
                    *q++=*p;
                p++;
            }
            *q = '\0';
        }
        escaped = HTEscape(unescaped, URL_DOSFILE);
        HT_FREE(unescaped);

#else  /* Unix */
        char * escaped_path = HTEscape(local, URL_PATH);
        escaped = StrAllocMCopy(&escaped, scheme, escaped_path, NULL);
        HT_FREE(escaped_path);

#endif /* not WIN32 */
#endif /* not VMS */
    }
    return escaped;
}
Beispiel #3
0
/*	Convert file URLs into a local representation
**	---------------------------------------------
**	The URL has already been translated through the rules in get_physical
**	in HTAccess.c and all we need to do now is to map the path to a local
**	representation, for example if must translate '/' to the ones that
**	turn the wrong way ;-)
**	Returns:
**		OK:	local file (that must be freed by caller)
**		Error:	NULL
*/
PUBLIC char * HTWWWToLocal (const char * url, const char * base,
                            HTUserProfile * up)
{
    if (url) {
        char * access = HTParse(url, base, PARSE_ACCESS);
        char * host = HTParse(url, base, PARSE_HOST);
        char * path = HTParse(url, base, PARSE_PATH+PARSE_PUNCTUATION);
        const char * myhost = HTUserProfile_fqdn(up);

        /* Find out if this is a reference to the local file system */
        if ((*access && strcmp(access, "file") && strcmp(access, "cache")) ||
                (*host && strcasecomp(host, "localhost") &&
                 myhost && strcmp(host, myhost))) {
            HTTRACE(CORE_TRACE, "LocalName... Not on local file system\n");
            HT_FREE(access);
            HT_FREE(host);
            HT_FREE(path);
            return NULL;
        } else {
            char *ptr;
            if ((ptr = strchr(path, ';')) || (ptr = strchr(path, '?')))
                *ptr = '\0';

            /*
            ** Do whatever translation is required here in order to fit your
            ** platform _before_ the path is unescaped.
            */
#ifdef VMS
            HTVMS_checkDecnet(path);
#endif
#ifdef WWW_MSWINDOWS
            /* An absolute pathname with logical drive */
            if (*path == '/' && path[2] == ':') {
                char *orig=path, *dest=path+1;
                while((*orig++ = *dest++));

                /* A network host */
            } else if (*host && strcasecomp(host, "localhost")) {
                char * newpath = NULL;
                StrAllocMCopy(&newpath, "//", host, path, NULL);
                HT_FREE(path);
                path = newpath;
            }

            /* Convert '/' to '\' */
            {
                char *p = path;
                while (*p) {
                    if (*p=='/') *p='\\';
                    p++;
                }
            }
#endif

            HTUnEscape(path);		  /* Take out the escaped characters */
            HTTRACE(CORE_TRACE, "Node........ `%s' means path `%s'\n" _ url _ path);
            HT_FREE(access);
            HT_FREE(host);
            return path;
        }
    }
    return NULL;
}
Beispiel #4
0
PUBLIC char * HTDialog_progressMessage (HTRequest * request, HTAlertOpcode op,
			                int msgnum, const char * dfault,
					void * input)
{
    char * result = NULL;
    switch (op) {
      case HT_PROG_DNS:
	StrAllocMCopy(&result, "Looking up ",
		      input ? (char *) input : "",
		      NULL);
	break;

      case HT_PROG_CONNECT:
	StrAllocMCopy(&result, "Contacting ",
		      input ? (char *) input : "",
		      NULL);
	break;

      case HT_PROG_ACCEPT:
	StrAllocCopy(result, "Waiting for connection...");
	break;

      case HT_PROG_LOGIN:
	StrAllocCopy(result, "Logging in...");
	break;

      case HT_PROG_READ:
	if (request) {
	    long cl = HTAnchor_length(HTRequest_anchor(request));
	    if (cl > 0) {
		long b_read = HTRequest_bodyRead(request);
		double pro = (double) b_read/cl*100;
		char buf[10];
		char pct[10];
		HTNumToStr((unsigned long) cl, buf, 10);
		sprintf(pct, "%d%%", (int) pro);
		StrAllocMCopy(&result, "Read (", pct, " of ", buf, ")", NULL);
	    } else {
		long b_read = HTRequest_bytesRead(request);
		int * raw_read = input ? (int *) input : NULL;
		if (b_read > 0) {
		    char buf[10];
		    HTNumToStr(b_read, buf, 10);
		    StrAllocMCopy(&result, "Read ", buf, "bytes", NULL);
		} else if (raw_read && *raw_read>0) {
		    char buf[10];
		    HTNumToStr(*raw_read, buf, 10);
		    StrAllocMCopy(&result, "Read ", buf, "bytes", NULL);
		} else {
		    StrAllocCopy(result, "Reading...");
		}
	    }
	}
	break;

      case HT_PROG_WRITE:
	if (request && HTMethod_hasEntity(HTRequest_method(request))) {
	    HTParentAnchor *anchor=HTRequest_anchor(HTRequest_source(request));
	    long cl = HTAnchor_length(anchor);
	    if (cl > 0) {
		long b_write = HTRequest_bodyWritten(request);
		double pro = (double) b_write/cl*100;
		char buf[10];
		char pct[10];
		HTNumToStr((unsigned long) cl, buf, 10);
		sprintf(pct, "%d%%", (int) pro);
		StrAllocMCopy(&result, "Writing (", pct, " of ", buf, ")", NULL);
	    } else {
		long b_written = HTRequest_bytesWritten(request);
		int * raw_written = input ? (int *) input : NULL;
		if (b_written > 0) {
		    char buf[10];
		    HTNumToStr(b_written>0 ? b_written : 0, buf, 10);
		    StrAllocMCopy(&result, "Writing ", buf, "bytes", NULL);
		} if (raw_written && *raw_written>0) {
		    char buf[10];
		    HTNumToStr(*raw_written, buf, 10);
		    StrAllocMCopy(&result, "Writing ", buf, "bytes", NULL);
		} else {
		    StrAllocCopy(result, "Writing...");
		}
	    }
        }
	break;

      case HT_PROG_DONE:
	StrAllocCopy(result, "Done!");
	break;

      case HT_PROG_INTERRUPT:
	StrAllocCopy(result, "Interrupted!");
	break;

      case HT_PROG_OTHER:
	StrAllocCopy(result, "Working - please wait...");
	break;

      case HT_PROG_TIMEOUT:
	StrAllocCopy(result, "Request timeout - server did not respond.");
	break;

      default:
	StrAllocCopy(result, "UNKNOWN PROGRESS STATE");
	break;
    }
    return result;
}
Beispiel #5
0
int main (int argc, char ** argv)
{
    HTSQL *	sql = NULL;
    char *	sqlserver = DEFAULT_SQL_SERVER;
    char *	sqldb = DEFAULT_SQL_DB;
    char *	sqluser = DEFAULT_SQL_USER;
    char *	sqlpw = DEFAULT_SQL_PW;
    char *	cvsuser = DEFAULT_CVS_USER;
    time_t	cvsdate = -1;
    FILE * fin = stdin;
    char *	input_buffer[BUFSIZE];
    HTChunk *	loginfo = HTChunk_new(BUFSIZE);
    int		arg = 0;
    BOOL	create_db = YES;

    /* Initiate libwww */
    HTLibInit(APP_NAME, APP_VERSION);

    /* Scan command line for parameters */
    for (arg=1; arg<argc; arg++) {
        if (*argv[arg] == '-') {

            if (!strcmp(argv[arg], "-h") || !strcmp(argv[arg], "-?")) {
                VersionInfo();
                Cleanup(0, sql, loginfo);

            } else if (!strcmp(argv[arg], "-v")) {
                HTSetTraceMessageMask("q");

            } else if (!strcmp(argv[arg], "-nocreate")) {
                create_db = NO;

            } else if (!strncmp(argv[arg], "-sqldb", 5)) {
                sqldb = (arg+1 < argc && *argv[arg+1] != '-') ?
                        argv[++arg] : DEFAULT_SQL_DB;

            } else if (!strncmp(argv[arg], "-sqlpassword", 5)) {
                sqlpw = (arg+1 < argc && *argv[arg+1] != '-') ?
                        argv[++arg] : DEFAULT_SQL_PW;

            } else if (!strncmp(argv[arg], "-sqlserver", 5)) {
                sqlserver = (arg+1 < argc && *argv[arg+1] != '-') ?
                            argv[++arg] : DEFAULT_SQL_SERVER;

            } else if (!strncmp(argv[arg], "-sqluser", 5)) {
                sqluser = (arg+1 < argc && *argv[arg+1] != '-') ?
                          argv[++arg] : DEFAULT_SQL_USER;

            } else if (!strncmp(argv[arg], "-cvsuser", 5)) {
                cvsuser = (arg+1 < argc && *argv[arg+1] != '-') ?
                          argv[++arg] : DEFAULT_CVS_USER;

            } else if (!strncmp(argv[arg], "-cvsdate", 5)) {
                cvsdate = (arg+1 < argc && *argv[arg+1] != '-') ?
                          HTParseTime(argv[++arg], NULL, NO) : -1;

            } else {
                fprintf(stderr, "Bad Argument (%s)\n", argv[arg]);
            }
        } else {
            fprintf(stderr, "Bad Argument (%s)\n", argv[arg]);
        }
    }

    /* Get an SQL object */
    if ((sql = HTSQL_new(sqlserver, sqluser, sqlpw, 0)) == NULL)
        Cleanup(-1, sql, loginfo);

    /* Connect to the SQL server */
    if (HTSQL_connect(sql) != YES) Cleanup(-1, sql, loginfo);

    /* Select our database */
    if (HTSQL_selectDB(sql, sqldb) != YES) Cleanup(-1, sql, loginfo);

    /* Create our tables */
    if (create_db) createTables(sql, 0);

    /* Read the arguments from stdin */
    for (;;) {
        int status = fread(input_buffer, 1, BUFSIZE, fin);
        if (status < 0) Cleanup(-1, sql, loginfo);
        if (status == 0) break;
        HTChunk_putb(loginfo, (const char *) input_buffer, status);
    }

    /* Parse the input chunk */
    {
        char * ptr = HTChunk_data(loginfo);
        char * noop1 = HTNextField(&ptr);
        char * noop2 = HTNextField(&ptr);
        char * root = HTNextField(&ptr);
        char * operation = NULL;
        char * files = NULL;
        char * comment = NULL;
        int comment_id = -1;
        int user_id = -1;
        char * p, * q;
#ifdef HT_REENTRANT
        char *lasts;					     /* For strtok_r */
#endif

        /* Find shared log message and get id */
        if ((q = HTStrCaseStr(ptr, "\nLog Message:")) != NULL) {
            comment = q+14;
            *q = '\0';
        }
        if ((comment_id = add_comment(sql, comment)) < 0)
            Cleanup(-1, sql, loginfo);

        /* Add/find user and get id */
        if ((user_id = add_user(sql, cvsuser)) < 0)
            Cleanup(-1, sql, loginfo);

        /* For each operation, find the files involved */
        while ((q = HTStrCaseStr(ptr, " Files:")) != NULL) {

            /* Find the operation */
            files = q+9;
            for (p=q; p>HTChunk_data(loginfo) && *p && *p!='\n'; p--);
            p++;
            operation = HTNextField(&p);

            /* Find the next line */
            if ((q = strchr(files, '\n')) != NULL) {
                *q++ = '\0';
                ptr = q;
            }

            /* Create the query */
            if (operation && files && comment) {
                char * file;
                int location_id = -1;

#ifdef HT_REENTRANT
                if ((file = strtok_r(files, DELIMITERS, &lasts)) != NULL) {
#else
                if ((file = strtok(files, DELIMITERS)) != NULL) {
#endif /* HT_REENTRANT */
                    do {
                        char * path = NULL;
                        StrAllocMCopy(&path, root, "/", file, NULL);

                        /* Add/find location and get id */
                        if ((location_id = add_location(sql, path)) < 0) {
                            Cleanup(-1, sql, loginfo);
                            break;
                        }

#if 0
                        fprintf(stderr, "location: `%s\', user: `%s\', operation: `%s\', comment: `%s\'\n",
                                path, cvsuser, operation, comment);
#endif

                        /* Add log entry */
                        {
                            char buf[16384];
                            char * query = HTSQL_printf(buf, 16384, "insert into %s values (%u,%u,%T,%S,%u)",
                                                        DEFAULT_SQL_LOG_TABLE,
                                                        location_id,
                                                        user_id,
                                                        cvsdate,
                                                        operation,
                                                        comment_id);
                            if (HTSQL_query(sql, query) != YES) {
                                Cleanup(-1, sql, loginfo);
                                break;
                            }
                        }

                        HT_FREE(path);
#ifdef HT_REENTRANT
                    } while ((file = (char *) strtok_r(NULL, DELIMITERS, &lasts)) != NULL);
#else
                    }
                    while ((file = strtok(NULL, DELIMITERS)) != NULL);
#endif /* HT_REENTRANT */
                }
            }
        }
    }

    return 0;
}