• Main Page
  • Related Pages
  • Namespaces
  • Data Structures
  • Files
  • File List
  • Globals

NetTest-http-server.c

Go to the documentation of this file.
00001 /** @file
00002 
00003   A brief file description
00004 
00005   @section license License
00006 
00007   Licensed to the Apache Software Foundation (ASF) under one
00008   or more contributor license agreements.  See the NOTICE file
00009   distributed with this work for additional information
00010   regarding copyright ownership.  The ASF licenses this file
00011   to you under the Apache License, Version 2.0 (the
00012   "License"); you may not use this file except in compliance
00013   with the License.  You may obtain a copy of the License at
00014 
00015       http://www.apache.org/licenses/LICENSE-2.0
00016 
00017   Unless required by applicable law or agreed to in writing, software
00018   distributed under the License is distributed on an "AS IS" BASIS,
00019   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00020   See the License for the specific language governing permissions and
00021   limitations under the License.
00022  */
00023 
00024 
00025 
00026 IOBufferBlock *resp_blk;
00027 int doc_len;
00028 
00029 struct NetTesterSM:public Continuation
00030 {
00031   VIO *read_vio;
00032   IOBufferReader *reader, *resp_reader;
00033   NetVConnection *vc;
00034   MIOBuffer *req_buf, *resp_buf;
00035   char request[2000];
00036   int req_len;
00037 
00038 
00039     NetTesterSM(ProxyMutex * _mutex, NetVConnection * _vc):Continuation(_mutex)
00040   {
00041     MUTEX_TRY_LOCK(lock, mutex, _vc->thread);
00042     ink_release_assert(lock);
00043     vc = _vc;
00044     Debug("net_test", "Accepted a connection");
00045     SET_HANDLER(&NetTesterSM::handle_read);
00046     req_buf = new_MIOBuffer(1);
00047     reader = req_buf->alloc_reader();
00048     read_vio = vc->do_io_read(this, INT64_MAX, req_buf);
00049     //vc->set_inactivity_timeout(HRTIME_SECONDS(60));
00050     resp_buf = new_empty_MIOBuffer(6);
00051     resp_buf->append_block(resp_blk->clone());
00052     req_len = 0;
00053     resp_reader = resp_buf->alloc_reader();
00054   }
00055 
00056 
00057    ~NetTesterSM()
00058   {
00059     req_buf->dealloc_all_readers();
00060     req_buf->clear();
00061     free_MIOBuffer(req_buf);
00062 
00063     resp_buf->dealloc_all_readers();
00064     resp_buf->clear();
00065     free_MIOBuffer(resp_buf);
00066   }
00067 
00068   /* ********************* jtest sample request   **********************
00069      GET http://npdev:8080/0.5216393021/6000 HTTP/1.0
00070      Proxy-Connection: Keep-Alive
00071    */
00072 
00073   int handle_read(int event, void *data)
00074   {
00075     int r;
00076     char *str;
00077     switch (event) {
00078     case VC_EVENT_READ_READY:
00079       r = reader->read_avail();
00080       reader->read(&request[req_len], r);
00081       req_len += r;
00082       request[req_len] = 0;
00083       Debug("net_test", "%s\n", request);
00084       fflush(stdout);
00085       //vc->set_inactivity_timeout(HRTIME_SECONDS(30));
00086       if (strcmp(&request[req_len - 4], "\r\n\r\n") == 0) {
00087         Debug("net_test", "The request header is :\n%s\n", request);
00088         // parse and get the doc size
00089         SET_HANDLER(&NetTesterSM::handle_write);
00090         ink_assert(doc_len == resp_reader->read_avail());
00091         vc->do_io_write(this, doc_len, resp_reader);
00092         //vc->set_inactivity_timeout(HRTIME_SECONDS(10));
00093       }
00094       break;
00095     case VC_EVENT_READ_COMPLETE:
00096       /* FALLSTHROUGH */
00097     case VC_EVENT_EOS:
00098       r = reader->read_avail();
00099       str = new char[r + 10];
00100       reader->read(str, r);
00101       Debug("net_test", "%s", str);
00102       fflush(stdout);
00103     case VC_EVENT_ERROR:
00104     case VC_EVENT_INACTIVITY_TIMEOUT:
00105       vc->do_io_close();
00106       // fixme
00107       // handle timeout events
00108       break;
00109     default:
00110       ink_release_assert(!"unknown event");
00111 
00112     }
00113     return EVENT_CONT;
00114   }
00115 
00116 
00117   int handle_write(int event, Event * e)
00118   {
00119     switch (event) {
00120     case VC_EVENT_WRITE_READY:
00121       break;
00122 
00123     case VC_EVENT_WRITE_COMPLETE:
00124     case VC_EVENT_EOS:
00125     case VC_EVENT_ERROR:
00126     case VC_EVENT_INACTIVITY_TIMEOUT:
00127       vc->do_io_close();
00128       delete this;
00129       return EVENT_DONE;
00130       break;
00131     default:
00132       ink_release_assert(!"unknown event");
00133     }
00134     return EVENT_CONT;
00135   }
00136 
00137 
00138 
00139 };
00140 
00141 
00142 struct NetTesterAccept:public Continuation
00143 {
00144 
00145   NetTesterAccept(ProxyMutex * _mutex):Continuation(_mutex)
00146   {
00147     SET_HANDLER(&NetTesterAccept::handle_accept);
00148   }
00149 
00150   int handle_accept(int event, void *data)
00151   {
00152     Debug("net_test", "Accepted a connection\n");
00153     fflush(stdout);
00154     NetVConnection *vc = (NetVConnection *) data;
00155     new NetTesterSM(new_ProxyMutex(), vc);
00156     return EVENT_CONT;
00157   }
00158 
00159 
00160 };
00161 
00162 
00163 
00164 struct Stop:public Continuation
00165 {
00166   Action *a;
00167     Stop(ProxyMutex * m):Continuation(m)
00168   {
00169     SET_HANDLER(&Stop::stop);
00170   }
00171 
00172   int stop(int event, Event * e)
00173   {
00174     a->cancel();
00175     return EVENT_DONE;
00176   }
00177 };
00178 
00179 
00180 int
00181 test_main()
00182 {
00183   const char *response_hdr = "HTTP/1.0 200 OK\n" "Content-Type: text/html\n" "Content-Length: 8000\r\n\r\n";
00184 
00185   resp_blk = new_IOBufferBlock();
00186   resp_blk->alloc(6);
00187   char *b = resp_blk->start();
00188   ink_strlcpy(b, response_hdr, resp_blk->block_size());
00189   memset(b + strlen(response_hdr), 'x', 8000);
00190   resp_blk->fill(doc_len = strlen(response_hdr) + 8000);
00191 
00192   Action *a = sslNetProcessor.accept(new NetTesterAccept(new_ProxyMutex()), 8080, true);
00193 
00194   return 0;
00195 }

Generated by  doxygen 1.7.1