~ubuntu-branches/ubuntu/trusty/tomcat7/trusty-security

« back to all changes in this revision

Viewing changes to java/org/apache/coyote/AbstractProtocol.java

  • Committer: Package Import Robot
  • Author(s): tony mancill
  • Date: 2012-06-07 22:43:21 UTC
  • mfrom: (11.1.4 sid)
  • Revision ID: package-import@ubuntu.com-20120607224321-cfev8j681yueyov3
Tags: 7.0.27-1
New upstream release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
16
16
 */
17
17
package org.apache.coyote;
18
18
 
 
19
import java.io.IOException;
19
20
import java.net.InetAddress;
20
21
import java.util.concurrent.ConcurrentHashMap;
21
22
import java.util.concurrent.ConcurrentLinkedQueue;
28
29
import javax.management.MalformedObjectNameException;
29
30
import javax.management.ObjectName;
30
31
 
 
32
import org.apache.coyote.http11.upgrade.UpgradeInbound;
 
33
import org.apache.coyote.http11.upgrade.UpgradeProcessor;
31
34
import org.apache.juli.logging.Log;
32
35
import org.apache.tomcat.util.ExceptionUtils;
33
36
import org.apache.tomcat.util.modeler.Registry;
260
263
        }
261
264
        int port = getPort();
262
265
        if (port == 0) {
263
 
            // auto binding is in use so port is unknown at this point
 
266
            // Auto binding is in use. Check if port is known
264
267
            name.append("auto-");
265
268
            name.append(getNameIndex());
 
269
            port = getLocalPort();
 
270
            if (port != -1) {
 
271
                name.append('-');
 
272
                name.append(port);
 
273
            }
266
274
        } else {
267
275
            name.append(port);
268
276
        }
500
508
    
501
509
    // ------------------------------------------- Connection handler base class
502
510
    
503
 
    protected abstract static class AbstractConnectionHandler<S,P extends AbstractProcessor<S>>
 
511
    protected abstract static class AbstractConnectionHandler<S,P extends Processor<S>>
504
512
            implements AbstractEndpoint.Handler {
505
513
 
506
514
        protected abstract Log getLog();
508
516
        protected RequestGroupInfo global = new RequestGroupInfo();
509
517
        protected AtomicLong registerCount = new AtomicLong(0);
510
518
 
511
 
        protected ConcurrentHashMap<S,P> connections =
512
 
            new ConcurrentHashMap<S,P>();
 
519
        protected ConcurrentHashMap<S,Processor<S>> connections =
 
520
            new ConcurrentHashMap<S,Processor<S>>();
513
521
 
514
522
        protected RecycledProcessors<P,S> recycledProcessors =
515
523
            new RecycledProcessors<P,S>(this);
531
539
 
532
540
        public SocketState process(SocketWrapper<S> socket,
533
541
                SocketStatus status) {
534
 
            P processor = connections.remove(socket.getSocket());
535
 
 
536
 
            if (getLog().isDebugEnabled()) {
537
 
                getLog().debug("process() entry " +
538
 
                        "Socket: [" + logHashcode(socket.getSocket()) + "], " +
539
 
                        "Processor [" + logHashcode(processor) + "]");
540
 
            }
 
542
            Processor<S> processor = connections.remove(socket.getSocket());
541
543
 
542
544
            socket.setAsync(false);
543
545
 
549
551
                    processor = createProcessor();
550
552
                }
551
553
 
552
 
                if (getLog().isDebugEnabled()) {
553
 
                    getLog().debug("process() gotProcessor " +
554
 
                            "Socket: [" + logHashcode(socket.getSocket()) + "], " +
555
 
                            "Processor [" + logHashcode(processor) + "]");
556
 
                }
557
 
 
558
554
                initSsl(socket, processor);
559
555
 
560
556
                SocketState state = SocketState.CLOSED;
561
557
                do {
562
558
                    if (processor.isAsync() || state == SocketState.ASYNC_END) {
563
559
                        state = processor.asyncDispatch(status);
564
 
                        if (getLog().isDebugEnabled()) {
565
 
                            getLog().debug("process() asyncDispatch " +
566
 
                                    "Socket: [" + logHashcode(socket.getSocket()) + "], " +
567
 
                                    "Processor: [" + logHashcode(processor) + "], " +
568
 
                                    "State: [" + state.toString() + "]");
569
 
                        }
570
560
                    } else if (processor.isComet()) {
571
561
                        state = processor.event(status);
572
 
                        if (getLog().isDebugEnabled()) {
573
 
                            getLog().debug("process() event " +
574
 
                                    "Socket: [" + logHashcode(socket.getSocket()) + "], " +
575
 
                                    "Processor: [" + logHashcode(processor) + "], " +
576
 
                                    "State: [" + state.toString() + "]");
577
 
                        }
 
562
                    } else if (processor.isUpgrade()) {
 
563
                        state = processor.upgradeDispatch();
578
564
                    } else {
579
565
                        state = processor.process(socket);
580
 
                        if (getLog().isDebugEnabled()) {
581
 
                            getLog().debug("process() process " +
582
 
                                    "Socket: [" + logHashcode(socket.getSocket()) + "], " +
583
 
                                    "Processor: [" + logHashcode(processor) + "], " +
584
 
                                    "State: [" + state.toString() + "]");
585
 
                        }
586
566
                    }
587
567
    
588
568
                    if (state != SocketState.CLOSED && processor.isAsync()) {
589
569
                        state = processor.asyncPostProcess();
590
 
                        if (getLog().isDebugEnabled()) {
591
 
                            getLog().debug("process() asyncPostProcess " +
592
 
                                    "Socket: [" + logHashcode(socket.getSocket()) + "], " +
593
 
                                    "Processor: [" + logHashcode(processor) + "], " +
594
 
                                    "State: [" + state.toString() + "]");
595
 
                        }
 
570
                    }
596
571
 
 
572
                    if (state == SocketState.UPGRADING) {
 
573
                        // Get the UpgradeInbound handler
 
574
                        UpgradeInbound inbound = processor.getUpgradeInbound();
 
575
                        // Release the Http11 processor to be re-used
 
576
                        release(socket, processor, false, false);
 
577
                        // Create the light-weight upgrade processor
 
578
                        processor = createUpgradeProcessor(socket, inbound);
 
579
                        inbound.onUpgradeComplete();
597
580
                    }
598
 
                } while (state == SocketState.ASYNC_END);
 
581
                } while (state == SocketState.ASYNC_END ||
 
582
                        state == SocketState.UPGRADING);
599
583
 
600
584
                if (state == SocketState.LONG) {
601
585
                    // In the middle of processing a request/response. Keep the
611
595
                    // closed. If it works, the socket will be re-added to the
612
596
                    // poller
613
597
                    release(socket, processor, false, false);
 
598
                } else if (state == SocketState.UPGRADED) {
 
599
                    // Need to keep the connection associated with the processor
 
600
                    longPoll(socket, processor);
614
601
                } else {
615
602
                    // Connection closed. OK to recycle the processor.
616
 
                    release(socket, processor, true, false);
 
603
                    if (!(processor instanceof UpgradeProcessor)) {
 
604
                        release(socket, processor, true, false);
 
605
                    }
617
606
                }
618
607
                return state;
619
608
            } catch(java.net.SocketException e) {
635
624
                // less-than-verbose logs.
636
625
                getLog().error(sm.getString("ajpprotocol.proto.error"), e);
637
626
            }
638
 
            release(socket, processor, true, false);
 
627
            // Don't try to add upgrade processors back into the pool
 
628
            if (!(processor instanceof UpgradeProcessor)) {
 
629
                release(socket, processor, true, false);
 
630
            }
639
631
            return SocketState.CLOSED;
640
632
        }
641
633
        
642
 
        private String logHashcode (Object o) {
643
 
            if (o == null) {
644
 
                return "null";
645
 
            } else {
646
 
                return Integer.toString(o.hashCode());
647
 
            }
648
 
        }
649
 
 
650
634
        protected abstract P createProcessor();
651
 
        protected abstract void initSsl(SocketWrapper<S> socket, P processor);
652
 
        protected abstract void longPoll(SocketWrapper<S> socket, P processor);
653
 
        protected abstract void release(SocketWrapper<S> socket, P processor,
654
 
                boolean socketClosing, boolean addToPoller);
655
 
 
 
635
        protected abstract void initSsl(SocketWrapper<S> socket,
 
636
                Processor<S> processor);
 
637
        protected abstract void longPoll(SocketWrapper<S> socket,
 
638
                Processor<S> processor);
 
639
        protected abstract void release(SocketWrapper<S> socket,
 
640
                Processor<S> processor, boolean socketClosing,
 
641
                boolean addToPoller);
 
642
        protected abstract Processor<S> createUpgradeProcessor(
 
643
                SocketWrapper<S> socket,
 
644
                UpgradeInbound inbound) throws IOException;
656
645
 
657
646
        protected void register(AbstractProcessor<S> processor) {
658
647
            if (getProtocol().getDomain() != null) {
681
670
            }
682
671
        }
683
672
 
684
 
        protected void unregister(AbstractProcessor<S> processor) {
 
673
        protected void unregister(Processor<S> processor) {
685
674
            if (getProtocol().getDomain() != null) {
686
675
                synchronized (this) {
687
676
                    try {
688
 
                        RequestInfo rp =
689
 
                            processor.getRequest().getRequestProcessor();
 
677
                        Request r = processor.getRequest();
 
678
                        if (r == null) {
 
679
                            // Probably an UpgradeProcessor
 
680
                            return;
 
681
                        }
 
682
                        RequestInfo rp = r.getRequestProcessor();
690
683
                        rp.setGlobalProcessor(null);
691
684
                        ObjectName rpName = rp.getRpName();
692
685
                        if (getLog().isDebugEnabled()) {
703
696
        }
704
697
    }
705
698
    
706
 
    protected static class RecycledProcessors<P extends AbstractProcessor<S>, S>
707
 
            extends ConcurrentLinkedQueue<P> {
 
699
    protected static class RecycledProcessors<P extends Processor<S>, S>
 
700
            extends ConcurrentLinkedQueue<Processor<S>> {
708
701
 
709
702
        private static final long serialVersionUID = 1L;
710
703
        private transient AbstractConnectionHandler<S,P> handler;
715
708
        }
716
709
 
717
710
        @Override
718
 
        public boolean offer(P processor) {
 
711
        public boolean offer(Processor<S> processor) {
719
712
            int cacheSize = handler.getProtocol().getProcessorCache();
720
713
            boolean offer = cacheSize == -1 ? true : size.get() < cacheSize;
721
714
            //avoid over growing our cache or add after we have stopped
731
724
        }
732
725
    
733
726
        @Override
734
 
        public P poll() {
735
 
            P result = super.poll();
 
727
        public Processor<S> poll() {
 
728
            Processor<S> result = super.poll();
736
729
            if (result != null) {
737
730
                size.decrementAndGet();
738
731
            }
741
734
    
742
735
        @Override
743
736
        public void clear() {
744
 
            P next = poll();
 
737
            Processor<S> next = poll();
745
738
            while (next != null) {
746
739
                handler.unregister(next);
747
740
                next = poll();