Merge pull request #2309 from mitza-oci/warnings
[ACE_TAO.git] / ACE / examples / Web_Crawler / HTTP_URL.cpp
blob026ff4db1873c292d4aec41babb9745e8ddfc903
1 #include "ace/OS_NS_stdio.h"
2 #include "ace/OS_NS_string.h"
3 #include "URL_Visitor.h"
4 #include "Options.h"
5 #include "HTTP_URL.h"
6 #include <memory>
8 const ACE_URL_Addr &
9 HTTP_URL::url_addr () const
11 return this->url_addr_;
14 HTTP_URL::HTTP_URL (const ACE_URL_Addr &url_addr,
15 HTTP_URL *cp)
16 : url_addr_ (url_addr),
17 containing_page_ (cp == 0 ? this : cp)
19 ACE_DEBUG ((LM_DEBUG, "HTTP_URL %s\n", url_addr.addr_to_string ()));
22 ssize_t
23 HTTP_URL::send_request ()
25 size_t commandsize =
26 ACE_OS::strlen (this->url_addr ().get_path_name ())
27 + ACE_OS::strlen (this->url_addr ().get_host_name ())
28 + 20 // Extra
29 + 1 // NUL byte
30 + 16; // Protocol filler...
32 char *command;
33 ACE_NEW_RETURN (command,
34 char[commandsize],
35 -1);
37 // Ensure that the <command> memory is deallocated.
38 std::unique_ptr<char[]> cmd_ptr (command);
40 ACE_OS::sprintf (cmd_ptr.get (),
41 "GET /%s HTTP/1.1\r\n",
42 ACE_TEXT_ALWAYS_CHAR (this->url_addr ().get_path_name ()));
44 // Send the GET command to the connected server.
45 if (this->stream ().send_n (cmd_ptr.get (),
46 ACE_OS::strlen (cmd_ptr.get ()),
47 const_cast<ACE_Time_Value *>
48 (OPTIONS::instance ()->timeout ())) > 0)
50 ACE_OS::sprintf (cmd_ptr.get (),
51 "Host: %s\r\n\r\n",
52 this->url_addr ().get_host_name ());
54 // IMP: The length of teh command has to be sent!
55 ssize_t retval =
56 this->stream ().send_n (cmd_ptr.get (),
57 ACE_OS::strlen (cmd_ptr.get ()),
58 const_cast<ACE_Time_Value *>
59 (OPTIONS::instance ()->timeout ()));
60 this->stream ().svc_handler ()->idle (0);
61 if (retval <= 0)
62 return -1;
63 else
64 return retval;
66 else
67 return -1;
70 int
71 HTTP_URL::accept (URL_Visitor *visitor)
73 // This is part of the visitor pattern.
74 return visitor->visit (*this);
77 int
78 HTTP_URL::destroy ()
80 delete this;
81 return 0;
82 // Commit suicide!