1 /*
2 ** @(#) $Id$
3 **
4 ** More libwww samples can be found at "http://www.w3.org/Library/Examples/"
5 **
6 ** Copyright � 1995-1998 World Wide Web Consortium, (Massachusetts
7 ** Institute of Technology, Institut National de Recherche en
8 ** Informatique et en Automatique, Keio University). All Rights
9 ** Reserved. This program is distributed under the W3C's Software
10 ** Intellectual Property License. This program is distributed in the hope
11 ** that it will be useful, but WITHOUT ANY WARRANTY; without even the
12 ** implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
13 ** PURPOSE. See W3C License http://www.w3.org/Consortium/Legal/ for more
14 ** details.
15 **
16 ** Sample showing how to PUT a document to an HTTP server
17 */
18
19 #include "WWWLib.h"
20 #include "WWWInit.h"
21
printer(const char * fmt,va_list pArgs)22 PRIVATE int printer (const char * fmt, va_list pArgs)
23 {
24 return (vfprintf(stdout, fmt, pArgs));
25 }
26
tracer(const char * fmt,va_list pArgs)27 PRIVATE int tracer (const char * fmt, va_list pArgs)
28 {
29 return (vfprintf(stderr, fmt, pArgs));
30 }
31
terminate_handler(HTRequest * request,HTResponse * response,void * param,int status)32 PRIVATE int terminate_handler (HTRequest * request, HTResponse * response,
33 void * param, int status)
34 {
35 /* We are done with this request */
36 HTRequest_delete(request);
37
38 /* Terminate libwww */
39 HTProfile_delete();
40
41 exit(0);
42 }
43
main(int argc,char ** argv)44 int main (int argc, char ** argv)
45 {
46 HTRequest * request = NULL;
47 HTAnchor * src = NULL;
48 HTAnchor * dst = NULL;
49 char * src_str = NULL;
50 char * dst_str = NULL;
51 BOOL status = NO;
52
53 /* Create a new premptive client */
54 HTProfile_newNoCacheClient("libwww-PUT", "1.0");
55
56 /* Need our own trace and print functions */
57 HTPrint_setCallback(printer);
58 HTTrace_setCallback(tracer);
59
60 /* And the traces... */
61 #if 0
62 HTSetTraceMessageMask("sop");
63 #endif
64
65 /* Add our own filter to update the history list */
66 HTNet_addAfter(terminate_handler, NULL, NULL, HT_ALL, HT_FILTER_LAST);
67
68 /* Handle command line args */
69 if (argc >= 3) {
70 src_str = argv[1];
71 dst_str = argv[2];
72 } else {
73 HTPrint("Type the URI of the source and the URI of the destination.\n");
74 HTPrint("\t%s <src> <dst>\n", argv[0]);
75 HTPrint("For example, %s http://www.w3.org http://myserver/destination.html\n",
76 argv[0]);
77 return -1;
78 }
79
80 if (src_str && *src_str && dst_str && *dst_str) {
81
82 /* Make source relative to where we are */
83 char * cwd = HTGetCurrentDirectoryURL();
84 char * full_src_str = HTParse(src_str, cwd, PARSE_ALL);
85
86 HTPrint("Saving %s to %s\n", full_src_str, dst_str);
87
88 /* Create a request */
89 request = HTRequest_new();
90
91 /* Get an anchor object for the src and dest URIs */
92 src = HTAnchor_findAddress(full_src_str);
93 dst = HTAnchor_findAddress(dst_str);
94
95 /* PUT the source to the dest */
96 status = HTPutDocumentAnchor(HTAnchor_parent(src), dst, request);
97
98 /* We don't need these anymore */
99 HT_FREE(cwd);
100 HT_FREE(full_src_str);
101
102 /* Go into the event loop... */
103 if (status == YES) HTEventList_loop(request);
104
105 }
106
107 return 0;
108 }
109