Esempio n. 1
0
char *mget_cookie_create_request_header(const MGET_IRI *iri)
{
	int it, init = 0;
	time_t now = time(NULL);
	mget_buffer_t buf;

	debug_printf("cookie_create_request_header for host=%s path=%s\n",iri->host,iri->path);

	pthread_mutex_lock(&cookies_mutex);

	for (it = 0; it < mget_vector_size(cookies); it++) {
		MGET_COOKIE *cookie = mget_vector_get(cookies, it);

		if (((!cookie->host_only && domain_match(cookie->domain, iri->host)) ||
			(cookie->host_only && !strcasecmp(cookie->domain, iri->host))) &&
			(!cookie->expires || cookie->expires >= now) &&
			(!cookie->secure_only || (cookie->secure_only && iri->scheme == IRI_SCHEME_HTTPS)) &&
			path_match(cookie->path, iri->path))
		{
			if (!init) {
				mget_buffer_init(&buf, NULL, 128);
				init = 1;
			}

			if (buf.length)
				mget_buffer_printf_append2(&buf, "; %s=%s", cookie->name, cookie->value);
			else
				mget_buffer_printf_append2(&buf, "%s=%s", cookie->name, cookie->value);
		}
	}

	pthread_mutex_unlock(&cookies_mutex);

	return init ? buf.data : NULL;
}
Esempio n. 2
0
static void filter_refs(struct ref **refs, int nr_match, char **match)
{
	struct ref **return_refs;
	struct ref *newlist = NULL;
	struct ref **newtail = &newlist;
	struct ref *ref, *next;
	struct ref *fastarray[32];

	if (nr_match && !args.fetch_all) {
		if (ARRAY_SIZE(fastarray) < nr_match)
			return_refs = xcalloc(nr_match, sizeof(struct ref *));
		else {
			return_refs = fastarray;
			memset(return_refs, 0, sizeof(struct ref *) * nr_match);
		}
	}
	else
		return_refs = NULL;

	for (ref = *refs; ref; ref = next) {
		next = ref->next;
		if (!memcmp(ref->name, "refs/", 5) &&
		    check_ref_format(ref->name + 5))
			; /* trash */
		else if (args.fetch_all &&
			 (!args.depth || prefixcmp(ref->name, "refs/tags/") )) {
			*newtail = ref;
			ref->next = NULL;
			newtail = &ref->next;
			continue;
		}
		else {
			int order = path_match(ref->name, nr_match, match);
			if (order) {
				return_refs[order-1] = ref;
				continue; /* we will link it later */
			}
		}
		free(ref);
	}

	if (!args.fetch_all) {
		int i;
		for (i = 0; i < nr_match; i++) {
			ref = return_refs[i];
			if (ref) {
				*newtail = ref;
				ref->next = NULL;
				newtail = &ref->next;
			}
		}
		if (return_refs != fastarray)
			free(return_refs);
	}
	*refs = newlist;
}
Esempio n. 3
0
/*===========================================
 * filepath -- Find file in sequence of paths
 *  handles NULL in either argument
 *  returns alloc'd buffer
 * Warning: if called with mode other than "r" this may not 
 *         do what you want, because 1) if file is not found, it appends ext.
 *         and 2) file always goes in 1st specified directory of path unless
 *         name is absolute or ./something
 *=========================================*/
STRING
filepath (CNSTRING name, CNSTRING mode, CNSTRING path, CNSTRING  ext, INT utf8)
{
	char buf1[MAXPATHLEN], buf2[MAXPATHLEN];
	STRING p, q;
	INT nlen, elen;

	if (ISNULL(name)) return NULL;
	if (ISNULL(path)) return strsave(name);
	nlen = strlen(name);
	if (ext && *ext) {
		elen = strlen(ext);
		if ((nlen > elen) && path_match(name+nlen-elen, ext)) {
			ext = NULL;
			elen = 0;
		}
	}
	else { ext = NULL; elen = 0; }
	/*  at this point ext is non-null only if name doesn't 
	 *  end in ext.  I.E. we need to check for name + ext and name
	 */

	if (nlen + strlen(path) + elen >= MAXLINELEN) return NULL;

	/* for absolute and relative path names we first check the
	 * pathname for validity relative to the current directory
	 */
	if (is_path(name)) {
		/* If this is a path, i.e. it has multiple path elements
		 * use the name as is first.  So absolute paths don't
		 * get resolved by appending search paths.
		 */
		if (ext) {
			strcpy(buf1,name);
			nlen = strlen(buf1);
			strcat(buf1, ext);
			if (access(buf1, 0) == 0) return strsave(buf1);
			buf1[nlen] = '\0'; /* remove extension */
			if (access(buf1, 0) == 0) return strsave(buf1);
		}
		if (access(name, 0) == 0) return strsave(name);
		/* fail if get here and name begins with a / or ./
		 * as we didn't find the file.
		 * however let foo/bar sneak thru, so we allow access
		 * to files in subdirectories of the search path if 
		 * named in the filename.
		 */
		if (is_dir_sep(name[0]) || (name[0] == '.' && is_dir_sep(name[1])))
			return strsave(name);
#ifdef WIN32
		if (name[0] && name[1]==':' && isasciiletter(name[0])) {
			return strsave(name);
		}
#endif
	}

	/* it is a relative path, so search for it in search path */
	strcpy(buf1, path);
	zero_separate_path(buf1);
	p = buf1;
	while (*p) {
		q = buf2;
		strcpy(q, p);
		expand_special_fname_chars(buf2, sizeof(buf2), utf8);
		q += strlen(q);
		if (q>buf2 && !is_dir_sep(q[-1])) {
			strcpy(q, LLSTRDIRSEPARATOR);
			q++;
		}
		strcpy(q, name);
		if (ext) {
			nlen = strlen(buf2);
			strcat(buf2, ext);
			if (access(buf2, 0) == 0) return strsave(buf2);
			buf2[nlen] = '\0'; /* remove extension */
		}
		if (access(buf2, 0) == 0) return strsave(buf2);
		p += strlen(p);
		p++;
	}
	if (mode[0] == 'r') return NULL;
	p = buf1;
	q = buf2;
	strcpy(q, p);
	expand_special_fname_chars(buf2, sizeof(buf2), utf8);
	q += strlen(q);
	if (q>buf2 && !is_dir_sep(q[-1])) {
		strcpy(q, LLSTRDIRSEPARATOR);
		q++;
	}
	strcpy(q, name);
	if (ext) strcat(q, ext);
	return strsave(buf2);
}
Esempio n. 4
0
static FILE *
ask_for_file_worker (STRING mode,
                     STRING ttl,
                     STRING *pfname,
                     STRING *pfullpath,
                     STRING path,
                     STRING ext,
                     DIRECTION direction)
{
	FILE *fp;
	char prompt[MAXPATHLEN];
	char fname[MAXPATHLEN];
	int elen, flen;
	BOOLEAN rtn;

	make_fname_prompt(prompt, sizeof(prompt), ext);

	if (direction==INPUT)
		rtn = ask_for_input_filename(ttl, path, prompt, fname, sizeof(fname));
	else
		rtn = ask_for_output_filename(ttl, path, prompt, fname, sizeof(fname));
	
	if (pfname) {
		if (fname && fname[0])
			*pfname = strdup(fname);
		else
			*pfname = 0;
	}
	if (pfullpath) *pfullpath = 0; /* 0 indicates we didn't try to open */

	if (!rtn || !fname[0]) return NULL;

	if (!expand_special_fname_chars(fname, sizeof(fname), uu8)) {
		msg_error(_(qSfn2long));
		return NULL;
	}

ask_for_file_try:

	/* try name as given */
	if (ISNULL(path)) {
		/* bare filename was given */
		if ((fp = fopen(fname, mode)) != NULL) {
			if (pfname)
				strupdate(pfname, fname);
			return fp;
		}
	} else {
		/* fully qualified path was given */
		if ((fp = fopenpath(fname, mode, path, ext, uu8, pfullpath)) != NULL) {
			return fp;
		}
	}

	/* try default extension */
	if (ext) {
		elen = strlen(ext);
		flen = strlen(fname);
		if (elen<flen && path_match(fname+flen-elen, ext)) {
			ext = NULL;	/* the file name has the extension already */
		} else {
			/* add extension and go back and retry */
			llstrapps(fname, sizeof(fname), uu8, ext);
			ext = NULL; /* only append extension once! */
			goto ask_for_file_try;
		}
	}

	/* failed to open it, give up */
	msg_error(_(qSnofopn), fname);
	return NULL;
}