示例#1
0
文件: http.c 项目: alimon/http-tiny
int main(int argc,char* argv[]) 
{
	int  ret,lg,blocksize,r,i;
	char typebuf[70];
	char *data=NULL,*filename=NULL,*proxy=NULL;
	int data_len = 0;
	char *type = NULL;
	enum {
		ERR,
		DOPUT,
		DOGET,
		DODEL,
		DOHEA,
		DOPOST
	} todo=ERR;
	
	if (argc!=3) {
		fprintf(stderr,"usage: http <cmd> <url>\n\tby <*****@*****.**>\n");
		return 1;
	}

	i=1;
	 
	if (!strcasecmp(argv[i],"put")) {
		todo=DOPUT;
	} else if (!strcasecmp(argv[i],"get")) {
		todo=DOGET;
	} else if (!strcasecmp(argv[i],"delete")) {
		todo=DODEL;
	} else if (!strcasecmp(argv[i],"head")) {
		todo=DOHEA;
	} else if (!strcasecmp(argv[i],"post")) {
		todo=DOPOST;
	}

	if (todo==ERR) {
		fprintf(stderr,
			"Invalid <cmd> '%s',\nmust be "
			"'put', 'get', 'post', 'delete', or 'head'\n",
			argv[i]
		);
		return 2;
	}

	i++;
	
	if ((proxy=getenv("http_proxy"))) {
		ret=http_proxy_url(proxy);
		if (ret<0) { 
			return ret;
		}
	}
	
	ret=http_parse_url(argv[i],&filename);
	if (ret<0) {
		return ret;
	}
	
	switch (todo) {
	/* *** PUT  *** */
	case DOPUT:
		fprintf(stderr,"reading stdin...\n");
		/* read stdin into memory */
		blocksize=16384;
		lg=0;  

		if (!(data=malloc(blocksize))) {
			return 3;
		}

		while (1) {
			r=read(0,data+lg,blocksize-lg);
			if (r<=0) break; 
			lg+=r;
			if ((3*lg/2)>blocksize) {
				blocksize *= 4;
				fprintf(stderr,
				"read to date: %9d bytes, reallocating buffer to %9d\n",
				lg,blocksize);	
				if (!(data=realloc(data,blocksize))) {
					return 4;
				}
			}
		}

		fprintf(stderr,"read %d bytes\n",lg);
		ret=http_put(filename,data,lg,0,NULL);
		fprintf(stderr,"res=%d\n",ret);
		break;
	/* *** GET  *** */
	case DOGET:
		ret=http_get(filename,&data,&lg,typebuf);
		fprintf(stderr,"res=%d,type='%s',lg=%d\n",ret,typebuf,lg);
		fwrite(data,lg,1,stdout);
		fprintf(stderr, "%s\n", data);
		break;
	/* *** HEAD  *** */
	case DOHEA:
		ret=http_head(filename,&lg,typebuf);
		fprintf(stderr,"res=%d,type='%s',lg=%d\n",ret,typebuf,lg);
		break;
	/* *** DELETE  *** */
	case DODEL:
		ret=http_delete(filename);
		fprintf(stderr,"res=%d\n",ret);
		break;
	case DOPOST:
		ret = http_post(filename, "your_name=1", 11, NULL, &data, &data_len, &type);
		fprintf(stderr,"res=%d\n",ret);
		fprintf(stderr,"%s\n", type);
		fprintf(stderr,"data: %s\n", data);
		break;
	/* impossible... */
	default:
		fprintf(stderr,"impossible todo value=%d\n",todo);
		return 5;
	}
	
	if (type) {
		free(type);
	}

	if (data) {
		 free(data);
	}

	free(filename);
	 
	return ( (ret==201) || (ret==200) ) ? 0 : ret;
}
示例#2
0
文件: http.c 项目: Andy753421/thesis
/* HTTP Functions */
int http_parse(http_t *http, char ch)
{
	int mode = 0;

	if (http->mode < MD_POST) {
		if (ch == '\r')
			return 0;
		if (ch == ' ' && http->idx == 0)
			return 0;
	}

	switch (http->mode) {
		/* HTTP Parsing */
		case MD_METHOD:
			if (ch == ' ')
				http_mode(http, MD_PATH, http->req_method);
			else if (http->idx < MAX_METHOD)
				http->req_method[http->idx++] = ch;
			return 0;

		case MD_PATH:
			if (ch == ' ')
				http_mode(http, MD_VERSION, http->req_path);
			else if (http->idx < MAX_PATH)
				http->req_path[http->idx++] = ch;
			return 0;

		case MD_VERSION:
			if (ch == '\n') {
				http_mode(http, MD_FIELD, http->req_version);
				http_open(http, http->req_method,
					      http->req_path,
					      http->req_version);
			}
			else if (http->idx < MAX_VERSION)
				http->req_version[http->idx++] = ch;
			return 0;

		case MD_FIELD:
			if (ch == '\n') {
				mode = http_body(http);
				http_mode(http, mode, http->hdr_field);
				switch (mode) {
					case MD_METHOD: return HTTP_DONE;
					case MD_SOCK:   return HTTP_SOCK;
					case MD_POST:   return HTTP_POST;
				}
			} else if (ch == ':')
				http_mode(http, MD_VALUE, http->hdr_field);
			else if (http->idx < MAX_FIELD)
				http->hdr_field[http->idx++] = ch;
			return 0;

		case MD_VALUE:
			if (ch == '\n') {
				http_mode(http, MD_FIELD, http->hdr_value);
				http_head(http, http->hdr_field, http->hdr_value);
			} else if (http->idx < MAX_VALUE)
				http->hdr_value[http->idx++] = ch;
			return 0;

		case MD_POST:
			return 0;

		case MD_SOCK:
			return 0;

		default:
			return 0;
	}
}