Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 177 lines (160 sloc) 5.339 kB
3bc1a0b @abhinavsingh added multipart handling with a hack right now, need to remove that, …
abhinavsingh authored
1 <?php
2 /**
3 * Jaxl (Jabber XMPP Library)
4 *
5 * Copyright (c) 2009-2012, Abhinav Singh <me@abhinavsingh.com>.
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 *
15 * * Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
18 * distribution.
19 *
20 * * Neither the name of Abhinav Singh nor the names of his
21 * contributors may be used to endorse or promote products derived
22 * from this software without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
27 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
28 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
29 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
30 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRIC
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
34 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 * POSSIBILITY OF SUCH DAMAGE.
36 *
37 */
38
39 require_once JAXL_CWD.'/core/jaxl_fsm.php';
40
41 class HTTPMultiPart extends JAXLFsm {
42
43 public $boundary = null;
44 public $form_data = array();
45 public $index = -1;
46
47 public function handle_invalid_state($r) {
48 _error("got invalid event $r");
49 }
50
51 public function __construct($boundary) {
52 $this->boundary = $boundary;
53 parent::__construct('wait_for_boundary_start');
54 }
55
56 public function state() {
57 return $this->state;
58 }
59
60 public function wait_for_boundary_start($event, $data) {
61 if($event == 'process') {
62 if($data[0] == '--'.$this->boundary) {
63 $this->index += 1;
64 $this->form_data[$this->index] = array(
971d90e @abhinavsingh fixed typo in muc_log_bot to catch various cases successfully
abhinavsingh authored
65 'meta' => array(),
66 'headers' => array(),
67 'body' => ''
3bc1a0b @abhinavsingh added multipart handling with a hack right now, need to remove that, …
abhinavsingh authored
68 );
69 return array('wait_for_content_disposition', true);
70 }
71 else {
72 _warning("invalid boundary start $data[0] while expecting $this->boundary");
73 return array('wait_for_boundary_start', false);
74 }
75 }
76 else {
77 _warning("invalid $event rcvd");
78 return array('wait_for_boundary_start', false);
79 }
80 }
81
82 public function wait_for_content_disposition($event, $data) {
83 if($event == 'process') {
84 $disposition = explode(":", $data[0]);
85
86 if(strtolower(trim($disposition[0])) == 'content-disposition') {
87 $this->form_data[$this->index]['headers'][$disposition[0]] = trim($disposition[1]);
88 $meta = explode(";", $disposition[1]);
89 if(trim(array_shift($meta)) == 'form-data') {
90 foreach($meta as $k) {
91 list($k, $v) = explode("=", $k);
92 $this->form_data[$this->index]['meta'][$k] = $v;
93 }
94
95 return array('wait_for_content_type', true);
96 }
97 else {
98 _warning("first part of meta is not form-data");
99 return array('wait_for_content_disposition', false);
100 }
101 }
102 else {
103 _warning("not a valid content-disposition line");
104 return array('wait_for_content_disposition', false);
105 }
106 }
107 else {
108 _warning("invalid $event rcvd");
109 return array('wait_for_content_disposition', false);
110 }
111 }
112
113 public function wait_for_content_type($event, $data) {
114 if($event == 'process') {
115 $type = explode(":", $data[0]);
116 if(strtolower(trim($type[0])) == 'content-type') {
117 $this->form_data[$this->index]['headers'][$type[0]] = trim($type[1]);
118 $this->form_data[$this->index]['meta']['type'] = $type[1];
119 return array('wait_for_content_body', true);
120 }
121 else {
122 _debug("not a valid content-type line");
123 return array('wait_for_content_type', false);
124 }
125 }
126 else {
127 _warning("invalid $event rcvd");
128 return array('wait_for_content_type', false);
129 }
130 }
131
132 public function wait_for_content_body($event, $data) {
133 if($event == 'process') {
134 if($data[0] == '--'.$this->boundary) {
135 _debug("start of new multipart/form-data detected");
136 return array('wait_for_content_disposition', true);
137 }
138 else if($data[0] == '--'.$this->boundary.'--') {
139 _debug("end of multipart form data detected");
140 return array('wait_for_empty_line', true);
141 }
142 else {
143 $this->form_data[$this->index]['body'] .= $data[0];
144 return array('wait_for_content_body', true);
145 }
146 }
147 else {
148 _warning("invalid $event rcvd");
149 return array('wait_for_content_body', false);
150 }
151 }
152
153 public function wait_for_empty_line($event, $data) {
154 if($event == 'process') {
155 if($data[0] == '') {
156 return array('done', true);
157 }
158 else {
159 _warning("invalid empty line $data[0] received");
160 return array('wait_for_empty_line', false);
161 }
162 }
163 else {
164 _warning("got $event in done state with data $data[0]");
165 return array('wait_for_empty_line', false);
166 }
167 }
168
169 public function done($event, $data) {
170 _warning("got unhandled event $event with data $data[0]");
171 return array('done', false);
172 }
173
174 }
175
176 ?>
Something went wrong with that request. Please try again.