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 POST data 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 HTParentAnchor * src = NULL;
48 HTAnchor * dst = NULL;
49 char * dst_str = NULL;
50 char * data = NULL;
51 BOOL status = NO;
52
53 /* Create a new premptive client */
54 HTProfile_newNoCacheClient("libwww-POST", "1.0");
55
56 /* Need our own trace and print functions */
57 HTPrint_setCallback(printer);
58 HTTrace_setCallback(tracer);
59
60 /* Add our own filter to update the history list */
61 HTNet_addAfter(terminate_handler, NULL, NULL, HT_ALL, HT_FILTER_LAST);
62
63 /* Handle command line args */
64 if (argc >= 3) {
65 dst_str = argv[1];
66 data = argv[2];
67 } else {
68 HTPrint("Type the URI of the destination you want to POST to and the contents that you want to post.\n");
69 HTPrint("\t%s <destination> <data>\n", argv[0]);
70 HTPrint("For example, %s http://myserver/destination.html \"This is some testdata\"\n",
71 argv[0]);
72 return -1;
73 }
74
75 if (data && *data && dst_str && *dst_str) {
76
77 /* Make source relative to where we are */
78 char * cwd = HTGetCurrentDirectoryURL();
79
80 HTPrint("Posting to %s\n", dst_str);
81
82 /* Create a request */
83 request = HTRequest_new();
84
85 /* Get an anchor object for the destination URI */
86 dst = HTAnchor_findAddress(dst_str);
87
88 /*
89 ** Dream up a source anchor (an editor can for example use this).
90 ** After creation we associate the data that we want to post and
91 ** set some metadata about what the data is. More formats can be found
92 ** ../src/HTFormat.html
93 */
94 src = HTTmpAnchor(NULL);
95 HTAnchor_setDocument(src, data);
96 HTAnchor_setFormat(src, WWW_PLAINTEXT);
97
98 /*
99 ** If not posting to an HTTP/1.1 server then content length MUST be
100 ** there. If HTTP/1.1 then it doesn't matter as we just use chunked
101 ** encoding under the covers
102 */
103 HTAnchor_setLength(src, strlen(data));
104
105 /* POST the source to the dest */
106 status = HTPostAnchor(src, dst, request);
107
108 /* We don't need these anymore */
109 HT_FREE(cwd);
110
111 /* Go into the event loop... */
112 if (status == YES) HTEventList_loop(request);
113
114 }
115
116 return 0;
117 }
118