summaryrefslogtreecommitdiff
path: root/examples/Web_Crawler/Iterators.cpp
blob: 199f427f0c73b4f9bd618c7fba8650957aa18299 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
// $Id$

#include "Options.h"
#include "Iterators.h"

ACE_RCSID(Web_Crawler, Iterators, "$Id$")

URL_Iterator::~URL_Iterator (void)
{
}

int
URL_Iterator::destroy (void)
{
  // Commit suicide.
  delete this;
  return 0;
}

HTML_Body_Iterator::HTML_Body_Iterator (URL &url)
  : url_ (url)
{
}

int
HTML_Body_Iterator::next (ACE_CString &url)
{
  size_t len = BUFSIZ;
  const char *buf;
  ACE_CString buffer; 
  int href_index = 0;

  for (buf = this->url_.stream ().recv (len);
       buf > 0;
       buf = this->url_.stream ().recv (len))
    {

      buffer.set (buf, BUFSIZ, 1);

      href_index = buffer.find ("HREF");

      if (href_index < 0)
        href_index = buffer.find ("href");

      // Grep fpr " and grab the string until end-"
      if ( href_index > 0)
        {
          // Get back to buffer start location.
          this->url_.stream ().seek (-1 * len, SEEK_CUR);

          int start_index = buffer.find ('\"',
                                         href_index);
          if (start_index <= 0)
            break;

          start_index += href_index;

          int end_index = buffer.find ('\"',
                                       start_index + 1);
          if (end_index <= 0)
            break;

          end_index += start_index + 1;

          ssize_t url_len = end_index - (start_index + 1);

          ACE_CString temp = buffer.substring (start_index + 1, 
                                               url_len);
          url.set (temp.c_str (), len, 1);

          this->url_.stream ().seek (end_index + 1);

          return url_len;
        }
    }
  return 0;

}

HTTP_Header_Iterator::HTTP_Header_Iterator (URL &url)
  : url_ (url),
    end_of_header_ (0)
{
}

int
HTTP_Header_Iterator::next (ACE_CString &line)
{
  if (this->end_of_header_)
    return 0;
  else
    {
      for (char c;
           (c = this->url_.stream ().get_char ()) != EOF;
           )
        {
          // Check to see if we're at the end of the header line.
          if (c == '\r' && this->url_.stream ().peek_char (0) == '\n')
            {
              line.set (this->url_.stream ().recv (),
                        this->url_.stream ().recv_len () - 1,
                        1);

              // Check to see if we're at the end of the header.
              if (this->url_.stream ().peek_char (1) == '\r'
                  && this->url_.stream ().peek_char (2) == '\n')
                {
                  this->end_of_header_ = 1;
                  // We're at the end of the header section.
                  this->url_.stream ().seek (3);
                }
              else
                // We're at the end of the line.
                this->url_.stream ().seek (1);

              return 1;
            }
          // Handle broken Web servers that use '\n' instead of
          // '\r\n'.
          else if (c == '\n')
            {
              line.set (this->url_.stream ().recv (),
                        (this->url_.stream ().recv_len ()),
                        1);

              // Check to see if we're at the end of the header.
              if (this->url_.stream ().peek_char (0) == '\n')
                {
                  // We're at the end of the header section.
                  this->url_.stream ().seek (1);
                  this->end_of_header_ = 1;
                }

              return 1;
            }
        }
     
    }
  return 0;
}

URL_Download_Iterator::URL_Download_Iterator (URL &url)
  : url_ (url)
{
}

int
URL_Download_Iterator::next (ACE_CString &buffer)
{
  size_t len = BUFSIZ;

  const char *buf = this->url_.stream ().recv (len);


  if (buf == 0)
    return 0;
  else
    {
      buffer.set (buf, len, 1);
      return 1;
    }
}