1 23 package com.sun.enterprise.web.connector.grizzly.algorithms; 24 25 import com.sun.enterprise.web.connector.grizzly.Handler; 26 import com.sun.enterprise.web.connector.grizzly.SelectorThread; 27 import com.sun.enterprise.web.connector.grizzly.handlers.NoParsingHandler; 28 29 import java.nio.ByteBuffer ; 30 import java.nio.BufferUnderflowException ; 31 import java.util.logging.Level ; 32 33 42 public final class StateMachineAlgorithm extends StreamAlgorithmBase{ 43 44 45 47 48 public StateMachineAlgorithm() { 49 handler = new NoParsingHandler(getPort()); 50 } 51 52 53 68 public boolean parse(ByteBuffer byteBuffer){ 69 boolean isFound = false; 70 71 curLimit = byteBuffer.limit(); 72 curPosition = byteBuffer.position(); 73 74 if ( contentLength != -1 ){ 76 isFound = ((contentLength + headerLength) <= byteBuffer.position()); 77 78 if (isFound) 79 byteBuffer.flip(); 80 81 return isFound; 82 } 83 84 if (byteBuffer.position() == 0) 86 return false; 87 88 if (SelectorThread.logger().isLoggable(Level.FINEST)) 89 SelectorThread.logger().log(Level.FINEST,dump(byteBuffer)); 90 91 byteBuffer.flip(); 92 try { 93 byte c; 94 byte prev_c = ' '; 95 96 try{ 97 if ( state != 0 ){ 98 byteBuffer.position(lastStatePosition); 99 } 100 } catch (RuntimeException ex){ 101 state = 0; 104 lastStatePosition = -1; 105 } 106 107 while(byteBuffer.hasRemaining()) { 109 c = byteBuffer.get(); 110 111 128 switch(state) { 133 case 0: if (c == 0x47 || c == 0x67) 135 state = 2; 136 else if (c == 0x50 || c == 0x70) 137 state = 8; 138 else if (c != 0x0d && c != 0x0a) 139 state = 1; 140 break; 141 case 1: if (c == 0x0a || c == 0x0d) 143 state = 0; 144 break; 145 case 2: if (c == 0x45 || c == 0x65) 147 state = 3; 148 else state = 1; 149 break; 150 case 3: if (c == 0x54 || c == 0x74) 152 state = 4; 153 else state = 1; 154 break; 155 case 4: if ( c == 0x0a ){ 157 state = 5; 158 } 159 break; 160 case 5: if ( c == 0x0d || c == 0x0a){ 162 headerLength = byteBuffer.position(); 163 isFound = true; 164 return isFound; 165 } 166 else state = 4; 167 break; 168 case 8: if (c == 0x4F || c == 0x6F) 170 state = 9; 171 else state = 1; 172 break; 173 case 9: if (c == 0x73 || c == 0x53) 175 state = 10; 176 else state = 1; 177 break; 178 case 10: if (c == 0x74 || c == 0x54) 180 state = 11; 181 else state = 1; 182 break; 183 case 11: if (c == 0x0a || c == 0x0d) 185 state = 12; 186 else state = 11; 187 break; 188 case 12: if (c == 0x43 || c == 0x63) 190 state = 13; 191 else if (prev_c == 0x0a 192 && byteBuffer.position() == curPosition) { 193 headerLength = byteBuffer.position(); 194 isFound = true; 196 return isFound; 197 } else state = 12; 198 break; 199 case 13: if (c == 0x4F || c == 0x6F) 201 state = 14; 202 else state = 11; 203 break; 204 case 14: if (c == 0x4E || c == 0x6E) 206 state = 15; 207 else state = 11; 208 break; 209 case 15: if (c == 0x54 || c == 0x74) 211 state = 16; 212 else state = 11; 213 break; 214 case 16: if (c == 0x45 || c == 0x65) 216 state = 17; 217 else state = 11; 218 break; 219 case 17: if (c == 0x4E || c == 0x6E) 221 state = 18; 222 else state = 11; 223 break; 224 case 18: if (c == 0x54 || c == 0x74) 226 state = 19; 227 else state = 11; 228 break; 229 case 19: if (c == 0x2D || c == 0x2D) 231 state = 20; 232 else state = 11; 233 break; 234 case 20: if (c == 0x4C || c == 0x6C) 236 state = 21; 237 else state = 11; 238 break; 239 case 21: if (c == 0x45 || c == 0x65) 241 state = 22; 242 else state = 11; 243 break; 244 case 22: if (c == 0x4E || c == 0x6E) 246 state = 23; 247 else state = 11; 248 break; 249 case 23: if (c == 0x47 || c == 0x67) 251 state = 24; 252 else state = 11; 253 break; 254 case 24: if (c == 0x54 || c == 0x74) 256 state = 25; 257 else state = 11; 258 break; 259 case 25: if (c == 0x48 || c == 0x68) 261 state = 26; 262 else state = 11; 263 break; 264 case 26: if (c == 0x3a) 266 state = 27; 267 else state = 11; 268 break; 269 case 27: while (c < 0x30 || c > 0x39) { 271 c = byteBuffer.get(); 274 } 275 StringBuilder sb = new StringBuilder (); 276 while (c >= 0x30 && c <= 0x39) { 277 sb.append((char) c); 278 c = byteBuffer.get(); 279 } 280 contentLength = Integer.parseInt(sb.toString()); 281 if (c == 0x0d) 285 state = 29; 286 else state = 28; 287 break; 288 case 28: if (c == 0x0d) 290 state = 29; 291 break; 292 case 29: if (c == 0x0a) 294 state = 30; 295 else state = 28; 296 break; 297 case 30: if (c == 0x0d) 299 state = 31; 300 else state = 28; 301 break; 302 case 31: if (c == 0x0a){ 304 headerLength = byteBuffer.position(); 305 isFound = ((contentLength + headerLength) 307 <= byteBuffer.limit()); 308 return isFound; 309 } 310 else state = 28; 311 break; 312 default: 313 throw new IllegalArgumentException ("Unexpected state"); 314 } 315 prev_c = c; 316 } 317 318 if ( state == 0 ) { 320 isFound = true; 321 } 322 323 return isFound; 324 } catch (BufferUnderflowException bue) { 325 SelectorThread.logger().log(Level.SEVERE,"readTask.bufferunderflow", bue); 326 return false; 327 } finally { 328 if ( headerLength == -1 && (state > 0)){ 329 lastStatePosition = byteBuffer.limit(); 332 } 333 byteBuffer.limit(curLimit); 334 byteBuffer.position(curPosition); 335 336 if (isFound){ 337 byteBuffer.flip(); 338 } 339 } 340 } 341 342 343 346 public Handler getHandler(){ 347 ((NoParsingHandler)handler).attachChannel(socketChannel); 348 return handler; 349 } 350 351 352 355 public void recycle(){ 356 super.recycle(); 357 358 socketChannel = null; 359 if ( handler != null){ 360 ((NoParsingHandler)handler).attachChannel(null); 361 } 362 } 363 } 364 | Popular Tags |