~ps10gel/ubuntu/xenial/trafficserver/6.2.0

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
/** @file

  A brief file description

  @section license License

  Licensed to the Apache Software Foundation (ASF) under one
  or more contributor license agreements.  See the NOTICE file
  distributed with this work for additional information
  regarding copyright ownership.  The ASF licenses this file
  to you under the Apache License, Version 2.0 (the
  "License"); you may not use this file except in compliance
  with the License.  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
 */

#include <limits.h>
#include "Net.h"
#include "Disk.h"
#include "Main.h"
#include "DNS.h"
#include "OneWayTunnel.h"

struct TestProxy : Continuation {
  NetVConnection *vc;
  NetVConnection *remote;
  MIOBuffer *inbuf;
  MIOBuffer *outbuf;
  char *host, *url, *url_end;
  char s[256];

  int
  done()
  {
    if (inbuf)
      free_MIOBuffer(inbuf);
    if (outbuf)
      free_MIOBuffer(outbuf);
    if (vc)
      vc->do_io(VIO::CLOSE);
    if (remote)
      remote->do_io(VIO::CLOSE);
    delete this;
    return EVENT_DONE;
  }

  int
  startEvent(int event, VIO *vio)
  {
    if (event != VC_EVENT_READ_READY) {
      printf("TestProxy startEvent error %d\n", event);
      return done();
    }
    if (vio->buffer.mbuf->gets(s, 255)) {
      host    = s + 11;
      url     = strchr(host, '/');
      url_end = strchr(url, ' ');
      *url    = 0;
      dnsProcessor.gethostbyname(host, this);
      *url = '/';
      SET_HANDLER(dnsEvent);
      vc = (NetVConnection *)vio->vc_server;
      return EVENT_DONE;
    }
    return EVENT_CONT;
  }

  int
  dnsEvent(int event, HostEnt *ent)
  {
    if (!ent) {
      printf("TestProxy dnsEvent error %d\n", event);
      return done();
    }
    unsigned int ip = *(unsigned int *)ent->h_addr_list[0];
    netProcessor.connect(this, ip, 80);
    SET_HANDLER(connectEvent);
    return EVENT_DONE;
  }

  int
  connectEvent(int event, NetVConnection *aremote)
  {
    if (!aremote) {
      printf("TestProxy connectEvent error %d\n", event);
      return done();
    }
    remote = aremote;
    outbuf = new_MIOBuffer();
    remote->do_io(VIO::WRITE, this, INT64_MAX, outbuf);
    *url_end = 0;
    sprintf(outbuf->start, "GET %s HTTP/1.0\n\n\n", url);
    outbuf->fill(strlen(outbuf->start) + 1);
    printf("sending [%s]\n", outbuf->start);
    SET_HANDLER(sendEvent);
    return EVENT_CONT;
  }

  int
  sendEvent(int event, VIO *vio)
  {
    if (event != VC_EVENT_WRITE_READY) {
      printf("TestProxy sendEvent error %d\n", event);
      return done();
    }
    if (vio->buffer.size())
      return EVENT_CONT;
    new OneWayTunnel(remote, vc, this, TUNNEL_TILL_DONE, true, true, true);
    SET_HANDLER(tunnelEvent);
    return EVENT_DONE;
  }

  int
  tunnelEvent(int event, Continuation *cont)
  {
    (void)cont;
    if (event != VC_EVENT_EOS) {
      printf("TestProxy sendEvent error %d\n", event);
      return done();
    }
    remote = 0;
    vc     = 0;
    printf("sucessful proxy of %s\n", url);
    return done();
  }

  TestProxy(MIOBuffer *abuf) : Continuation(new_ProxyMutex()), vc(0), remote(0), inbuf(abuf), outbuf(0), host(0), url(0), url_end(0)
  {
    SET_HANDLER(startEvent);
  }
};

struct TestAccept : Continuation {
  int
  startEvent(int event, NetVConnection *e)
  {
    if (!event) {
      MIOBuffer *buf = new_MIOBuffer();
      e->do_io(VIO::READ, new TestProxy(buf), INT64_MAX, buf);
    } else {
      printf("TestAccept error %d\n", event);
      return EVENT_DONE;
    }
    return EVENT_CONT;
  }
  TestAccept() : Continuation(new_ProxyMutex()) { SET_HANDLER(startEvent); }
};

void
test()
{
  netProcessor.accept(new TestAccept, accept_port_number);
}