http.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*
  2. * Http put/get/.. standalone program using Http put mini lib
  3. * written by L. Demailly
  4. * (c) 1998 Laurent Demailly - http://www.demailly.com/~dl/
  5. * (c) 1996 Observatoire de Paris - Meudon - France
  6. * see LICENSE for terms, conditions and DISCLAIMER OF ALL WARRANTIES
  7. *
  8. * $Id: http.c,v 1.4 1998/09/23 06:11:55 dl Exp $
  9. *
  10. * $Log: http.c,v $
  11. * Revision 1.4 1998/09/23 06:11:55 dl
  12. * one more lint
  13. *
  14. * Revision 1.3 1998/09/23 06:03:45 dl
  15. * proxy support (old change which was not checked in back in 96)
  16. * contact, etc.. infos update
  17. *
  18. * Revision 1.2 1996/04/18 13:52:14 dl
  19. * strings.h->string.h
  20. *
  21. * Revision 1.1 1996/04/18 12:17:25 dl
  22. * Initial revision
  23. *
  24. */
  25. static char *rcsid="$Id: http.c,v 1.4 1998/09/23 06:11:55 dl Exp $";
  26. #include <sys/types.h>
  27. #include <sys/uio.h>
  28. #include <unistd.h>
  29. #include <stdio.h>
  30. #include <stdlib.h>
  31. #include <string.h>
  32. #include "http_lib.h"
  33. int main(argc,argv)
  34. int argc;
  35. char **argv;
  36. {
  37. int ret,lg,blocksize,r,i;
  38. char typebuf[70];
  39. char *data=NULL,*filename=NULL,*proxy=NULL;
  40. enum {
  41. ERR,
  42. DOPUT,
  43. DOGET,
  44. DODEL,
  45. DOHEA
  46. } todo=ERR;
  47. if (argc!=3) {
  48. fprintf(stderr,"usage: http <cmd> <url>\n\tby <L@Demailly.com>\n");
  49. return 1;
  50. }
  51. i=1;
  52. if (!strcasecmp(argv[i],"put")) {
  53. todo=DOPUT;
  54. } else if (!strcasecmp(argv[i],"get")) {
  55. todo=DOGET;
  56. } else if (!strcasecmp(argv[i],"delete")) {
  57. todo=DODEL;
  58. } else if (!strcasecmp(argv[i],"head")) {
  59. todo=DOHEA;
  60. }
  61. if (todo==ERR) {
  62. fprintf(stderr,
  63. "Invalid <cmd> '%s',\nmust be 'put', 'get', 'delete', or 'head'\n",
  64. argv[i]);
  65. return 2;
  66. }
  67. i++;
  68. if ((proxy=getenv("http_proxy"))) {
  69. ret=http_parse_url(proxy,&filename);
  70. if (ret<0) return ret;
  71. http_proxy_server=http_server;
  72. http_server=NULL;
  73. http_proxy_port=http_port;
  74. }
  75. ret=http_parse_url(argv[i],&filename);
  76. if (ret<0) {if (proxy) free(http_proxy_server); return ret;}
  77. switch (todo) {
  78. /* *** PUT *** */
  79. case DOPUT:
  80. fprintf(stderr,"reading stdin...\n");
  81. /* read stdin into memory */
  82. blocksize=16384;
  83. lg=0;
  84. if (!(data=malloc(blocksize))) {
  85. return 3;
  86. }
  87. while (1) {
  88. r=read(0,data+lg,blocksize-lg);
  89. if (r<=0) break;
  90. lg+=r;
  91. if ((3*lg/2)>blocksize) {
  92. blocksize *= 4;
  93. fprintf(stderr,
  94. "read to date: %9d bytes, reallocating buffer to %9d\n",
  95. lg,blocksize);
  96. if (!(data=realloc(data,blocksize))) {
  97. return 4;
  98. }
  99. }
  100. }
  101. fprintf(stderr,"read %d bytes\n",lg);
  102. ret=http_put(filename,data,lg,0,NULL);
  103. fprintf(stderr,"res=%d\n",ret);
  104. break;
  105. /* *** GET *** */
  106. case DOGET:
  107. ret=http_get(filename,&data,&lg,typebuf);
  108. fprintf(stderr,"res=%d,type='%s',lg=%d\n",ret,typebuf,lg);
  109. fwrite(data,lg,1,stdout);
  110. break;
  111. /* *** HEAD *** */
  112. case DOHEA:
  113. ret=http_head(filename,&lg,typebuf);
  114. fprintf(stderr,"res=%d,type='%s',lg=%d\n",ret,typebuf,lg);
  115. break;
  116. /* *** DELETE *** */
  117. case DODEL:
  118. ret=http_delete(filename);
  119. fprintf(stderr,"res=%d\n",ret);
  120. break;
  121. /* impossible... */
  122. default:
  123. fprintf(stderr,"impossible todo value=%d\n",todo);
  124. return 5;
  125. }
  126. if (data) free(data);
  127. free(filename);
  128. free(http_server);
  129. if (proxy) free(http_proxy_server);
  130. return ( (ret==201) || (ret==200) ) ? 0 : ret;
  131. }