Exemple #1
0
static byte
add_tfmtable(int *        const table, 
             int *        const count, 
             int          const value, 
             int          const max_count, 
             const char * const name) {
    
    integer i;
    for (i = 0; i < *count; i++) /* search for value in tfm table */
        if (table[i] == value) return (byte)i;
    if (*count >= max_count)
        pm_error("too many values in %s table", name) ;
    if (!fixrange(value))
        pm_error("%s %f for char %d out of range",
                 name, unfixword(value), car);
    table[*count] = value ;
    return (*count)++ ;
}
Exemple #2
0
/*
 * fd references a file which has been authorized & checked for relocations.
 * send back the headers & its contents.
 * includes checks for conditional requests & ranges.
 */
int
sendfd(HConnect *c, int fd, Dir *dir, HContent *type, HContent *enc)
{
	Qid qid;
	HRange *r;
	HContents conts;
	Hio *hout;
	char *boundary, etag[32];
	long mtime;
	ulong tr;
	int n, nw, multir, ok;
	vlong wrote, length;

	hout = &c->hout;
	length = dir->length;
	mtime = dir->mtime;
	qid = dir->qid;
	free(dir);

	/*
	 * figure out the type of file and send headers
	 */
	n = -1;
	r = nil;
	multir = 0;
	boundary = nil;
	if(c->req.vermaj){
		if(type == nil && enc == nil){
			conts = uriclass(c, c->req.uri);
			type = conts.type;
			enc = conts.encoding;
			if(type == nil && enc == nil){
				n = read(fd, c->xferbuf, HBufSize-1);
				if(n > 0){
					c->xferbuf[n] = '\0';
					conts = dataclass(c, c->xferbuf, n);
					type = conts.type;
					enc = conts.encoding;
				}
			}
		}
		if(type == nil)
			type = hmkcontent(c, "application", "octet-stream", nil);

		snprint(etag, sizeof(etag), "\"%lluxv%lux\"", qid.path, qid.vers);
		ok = checkreq(c, type, enc, mtime, etag);
		if(ok <= 0){
			close(fd);
			return ok;
		}

		/*
		 * check for if-range requests
		 */
		if(c->head.range == nil
		|| c->head.ifrangeetag != nil && !etagmatch(1, c->head.ifrangeetag, etag)
		|| c->head.ifrangedate != 0 && c->head.ifrangedate != mtime){
			c->head.range = nil;
			c->head.ifrangeetag = nil;
			c->head.ifrangedate = 0;
		}

		if(c->head.range != nil){
			c->head.range = fixrange(c->head.range, length);
			if(c->head.range == nil){
				if(c->head.ifrangeetag == nil && c->head.ifrangedate == 0){
					hprint(hout, "%s 416 Request range not satisfiable\r\n", hversion);
					hprint(hout, "Date: %D\r\n", time(nil));
					hprint(hout, "Server: Plan9\r\n");
					hprint(hout, "Content-Range: bytes */%lld\r\n", length);
					hprint(hout, "Content-Length: %d\r\n", STRLEN(BADRANGE));
					hprint(hout, "Content-Type: text/html\r\n");
					if(c->head.closeit)
						hprint(hout, "Connection: close\r\n");
					else if(!http11(c))
						hprint(hout, "Connection: Keep-Alive\r\n");
					hprint(hout, "\r\n");
					if(strcmp(c->req.meth, "HEAD") != 0)
						hprint(hout, "%s", BADRANGE);
					hflush(hout);
					writelog(c, "Reply: 416 Request range not satisfiable\n");
					close(fd);
					return 1;
				}
				c->head.ifrangeetag = nil;
				c->head.ifrangedate = 0;
			}
		}
		if(c->head.range == nil)
			hprint(hout, "%s 200 OK\r\n", hversion);
		else
			hprint(hout, "%s 206 Partial Content\r\n", hversion);

		hprint(hout, "Server: Plan9\r\n");
		hprint(hout, "Date: %D\r\n", time(nil));
		hprint(hout, "ETag: %s\r\n", etag);

		/*
		 * can't send some entity headers if partially responding
		 * to an if-range: etag request
		 */
		r = c->head.range;
		if(r == nil)
			hprint(hout, "Content-Length: %lld\r\n", length);
		else if(r->next == nil){
			hprint(hout, "Content-Range: bytes %ld-%ld/%lld\r\n", r->start, r->stop, length);
			hprint(hout, "Content-Length: %ld\r\n", r->stop - r->start);
		}else{
			multir = 1;
			boundary = hmkmimeboundary(c);
			hprint(hout, "Content-Type: multipart/byteranges; boundary=%s\r\n", boundary);
		}
		if(c->head.ifrangeetag == nil){
			hprint(hout, "Last-Modified: %D\r\n", mtime);
			if(!multir)
				printtype(hout, type, enc);
			if(c->head.fresh_thresh)
				hintprint(c, hout, c->req.uri, c->head.fresh_thresh, c->head.fresh_have);
		}

		if(c->head.closeit)
			hprint(hout, "Connection: close\r\n");
		else if(!http11(c))
			hprint(hout, "Connection: Keep-Alive\r\n");
		hprint(hout, "\r\n");
	}
	if(strcmp(c->req.meth, "HEAD") == 0){
		if(c->head.range == nil)
			writelog(c, "Reply: 200 file 0\n");
		else
			writelog(c, "Reply: 206 file 0\n");
		hflush(hout);
		close(fd);
		return 1;
	}

	/*
	 * send the file if it's a normal file
	 */
	if(r == nil){
		hflush(hout);

		wrote = 0;
		if(n > 0)
			wrote = write(hout->fd, c->xferbuf, n);
		if(n <= 0 || wrote == n){
			while((n = read(fd, c->xferbuf, HBufSize)) > 0){
				nw = write(hout->fd, c->xferbuf, n);
				if(nw != n){
					if(nw > 0)
						wrote += nw;
					break;
				}
				wrote += nw;
			}
		}
		writelog(c, "Reply: 200 file %lld %lld\n", length, wrote);
		close(fd);
		if(length == wrote)
			return 1;
		return -1;
	}

	/*
	 * for multipart/byterange messages,
	 * it is not ok for the boundary string to appear within a message part.
	 * however, it probably doesn't matter, since there are lengths for every part.
	 */
	wrote = 0;
	ok = 1;
	for(; r != nil; r = r->next){
		if(multir){
			hprint(hout, "\r\n--%s\r\n", boundary);
			printtype(hout, type, enc);
			hprint(hout, "Content-Range: bytes %ld-%ld/%lld\r\n", r->start, r->stop, length);
			hprint(hout, "Content-Length: %ld\r\n", r->stop - r->start);
			hprint(hout, "\r\n");
		}
		hflush(hout);

		if(seek(fd, r->start, 0) != r->start){
			ok = -1;
			break;
		}
		for(tr = r->stop - r->start + 1; tr; tr -= n){
			n = tr;
			if(n > HBufSize)
				n = HBufSize;
			if(read(fd, c->xferbuf, n) != n){
				ok = -1;
				goto breakout;
			}
			nw = write(hout->fd, c->xferbuf, n);
			if(nw != n){
				if(nw > 0)
					wrote += nw;
				ok = -1;
				goto breakout;
			}
			wrote += nw;
		}
	}
breakout:;
	if(r == nil){
		if(multir){
			hprint(hout, "--%s--\r\n", boundary);
			hflush(hout);
		}
		writelog(c, "Reply: 206 partial content %lld %lld\n", length, wrote);
	}else
		writelog(c, "Reply: 206 partial content, early termination %lld %lld\n", length, wrote);
	close(fd);
	return ok;
}