/** * Create a new default SIPRequest from the original request. Warning: the newly created * SIPRequest, shares the headers of this request but we generate any new headers that we need to * modify so the original request is umodified. However, if you modify the shared headers after * this request is created, then the newly created request will also be modified. If you want to * modify the original request without affecting the returned Request make sure you clone it * before calling this method. * * <p>Only required headers are copied. * * <ul> * <li>Contact headers are not included in the newly created request. Setting the appropriate * sequence number is the responsibility of the caller. * <li>RouteList is not copied for ACK and CANCEL * <li>Note that we DO NOT copy the body of the argument into the returned header. We do not * copy the content type header from the original request either. These have to be added * seperately and the content length has to be correctly set if necessary the content length * is set to 0 in the returned header. * <li>Contact List is not copied from the original request. * <li>RecordRoute List is not included from original request. * <li>Via header is not included from the original request. * </ul> * * @param requestLine is the new request line. * @param switchHeaders is a boolean flag that causes to and from headers to switch (set this to * true if you are the server of the transaction and are generating a BYE request). If the * headers are switched, we generate new From and To headers otherwise we just use the * incoming headers. * @return a new Default SIP Request which has the requestLine specified. */ public SIPRequest createSIPRequest(RequestLine requestLine, boolean switchHeaders) { SIPRequest newRequest = new SIPRequest(); newRequest.requestLine = requestLine; Iterator headerIterator = this.getHeaders(); while (headerIterator.hasNext()) { SIPHeader nextHeader = (SIPHeader) headerIterator.next(); // For BYE and cancel set the CSeq header to the // appropriate method. if (nextHeader instanceof CSeq) { CSeq newCseq = (CSeq) nextHeader.clone(); nextHeader = newCseq; try { newCseq.setMethod(requestLine.getMethod()); } catch (ParseException e) { } } else if (nextHeader instanceof ViaList) { Via via = (Via) (((ViaList) nextHeader).getFirst().clone()); via.removeParameter("branch"); nextHeader = via; // Cancel and ACK preserve the branch ID. } else if (nextHeader instanceof To) { To to = (To) nextHeader; if (switchHeaders) { nextHeader = new From(to); ((From) nextHeader).removeTag(); } else { nextHeader = (SIPHeader) to.clone(); ((To) nextHeader).removeTag(); } } else if (nextHeader instanceof From) { From from = (From) nextHeader; if (switchHeaders) { nextHeader = new To(from); ((To) nextHeader).removeTag(); } else { nextHeader = (SIPHeader) from.clone(); ((From) nextHeader).removeTag(); } } else if (nextHeader instanceof ContentLength) { ContentLength cl = (ContentLength) nextHeader.clone(); try { cl.setContentLength(0); } catch (InvalidArgumentException e) { } nextHeader = cl; } else if (!(nextHeader instanceof CallID) && !(nextHeader instanceof MaxForwards)) { // Route is kept by dialog. // RR is added by the caller. // Contact is added by the Caller // Any extension headers must be added // by the caller. continue; } try { newRequest.attachHeader(nextHeader, false); } catch (SIPDuplicateHeaderException e) { e.printStackTrace(); } } if (MessageFactoryImpl.getDefaultUserAgentHeader() != null) { newRequest.setHeader(MessageFactoryImpl.getDefaultUserAgentHeader()); } return newRequest; }
/** * This is input reading thread for the pipelined parser. You feed it input through the input * stream (see the constructor) and it calls back an event listener interface for message * processing or error. It cleans up the input - dealing with things like line continuation */ public void run() { Pipeline inputStream = this.rawInputStream; // inputStream = new MyFilterInputStream(this.rawInputStream); // I cannot use buffered reader here because we may need to switch // encodings to read the message body. try { while (true) { this.sizeCounter = this.maxMessageSize; // this.messageSize = 0; StringBuffer inputBuffer = new StringBuffer(); if (Debug.parserDebug) Debug.println("Starting parse!"); String line1; String line2 = null; while (true) { try { line1 = readLine(inputStream); // ignore blank lines. if (line1.equals("\n")) { if (Debug.parserDebug) Debug.println("Discarding " + line1); continue; } else break; } catch (IOException ex) { Debug.printStackTrace(ex); this.rawInputStream.stopTimer(); return; } } inputBuffer.append(line1); // Guard against bad guys. this.rawInputStream.startTimer(); while (true) { try { line2 = readLine(inputStream); inputBuffer.append(line2); if (line2.trim().equals("")) break; } catch (IOException ex) { this.rawInputStream.stopTimer(); Debug.printStackTrace(ex); return; } } // Stop the timer that will kill the read. this.rawInputStream.stopTimer(); inputBuffer.append(line2); StringMsgParser smp = new StringMsgParser(sipMessageListener); smp.readBody = false; SIPMessage sipMessage = null; try { sipMessage = smp.parseSIPMessage(inputBuffer.toString()); if (sipMessage == null) { this.rawInputStream.stopTimer(); continue; } } catch (ParseException ex) { // Just ignore the parse exception. continue; } if (Debug.parserDebug) Debug.println("Completed parsing message"); ContentLength cl = (ContentLength) sipMessage.getContentLength(); int contentLength = 0; if (cl != null) { contentLength = cl.getContentLength(); } else { contentLength = 0; } if (Debug.parserDebug) { Debug.println("contentLength " + contentLength); Debug.println("sizeCounter " + this.sizeCounter); Debug.println("maxMessageSize " + this.maxMessageSize); } if (contentLength == 0) { sipMessage.removeContent(); } else if (maxMessageSize == 0 || contentLength < this.sizeCounter) { byte[] message_body = new byte[contentLength]; int nread = 0; while (nread < contentLength) { // Start my starvation timer. // This ensures that the other end // writes at least some data in // or we will close the pipe from // him. This prevents DOS attack // that takes up all our connections. this.rawInputStream.startTimer(); try { int readlength = inputStream.read(message_body, nread, contentLength - nread); if (readlength > 0) { nread += readlength; } else { break; } } catch (IOException ex) { ex.printStackTrace(); break; } finally { // Stop my starvation timer. this.rawInputStream.stopTimer(); } } sipMessage.setMessageContent(message_body); } // Content length too large - process the message and // return error from there. if (sipMessageListener != null) { try { sipMessageListener.processMessage(sipMessage); } catch (Exception ex) { // fatal error in processing - close the // connection. break; } } } } finally { try { inputStream.close(); } catch (IOException e) { InternalErrorHandler.handleException(e); } } }
/** * Creates a default ACK SIPRequest message for this original request. Note that the defaultACK * SIPRequest does not include the content of the original SIPRequest. If responseToHeader is null * then the toHeader of this request is used to construct the ACK. Note that tag fields are just * copied from the original SIP Request. Added by Jeff Keyser. * * @param responseToHeader To header to use for this request. * @return A SIPRequest with an ACK method. */ public SIPRequest createAckRequest(To responseToHeader) { SIPRequest newRequest; Iterator headerIterator; SIPHeader nextHeader; newRequest = new SIPRequest(); newRequest.setRequestLine((RequestLine) this.requestLine.clone()); newRequest.setMethod(Request.ACK); headerIterator = getHeaders(); while (headerIterator.hasNext()) { nextHeader = (SIPHeader) headerIterator.next(); if (nextHeader instanceof RouteList) { // Ack and cancel do not get ROUTE headers. // Route header for ACK is assigned by the // Dialog if necessary. continue; } else if (nextHeader instanceof ProxyAuthorization) { // Remove proxy auth header. // Assigned by the Dialog if necessary. continue; } else if (nextHeader instanceof ContentLength) { // Adding content is responsibility of user. nextHeader = (SIPHeader) nextHeader.clone(); try { ((ContentLength) nextHeader).setContentLength(0); } catch (InvalidArgumentException e) { } } else if (nextHeader instanceof ContentType) { // Content type header is removed since // content length is 0. continue; } else if (nextHeader instanceof CSeq) { // The CSeq header field in the // ACK MUST contain the same value for the // sequence number as was present in the // original request, but the method parameter // MUST be equal to "ACK". CSeq cseq = (CSeq) nextHeader.clone(); try { cseq.setMethod(Request.ACK); } catch (ParseException e) { } nextHeader = cseq; } else if (nextHeader instanceof To) { if (responseToHeader != null) { nextHeader = responseToHeader; } else { nextHeader = (SIPHeader) nextHeader.clone(); } } else if (nextHeader instanceof ContactList || nextHeader instanceof Expires) { // CONTACT header does not apply for ACK requests. continue; } else if (nextHeader instanceof ViaList) { // Bug reported by Gianluca Martinello // The ACK MUST contain a single Via header field, // and this MUST be equal to the top Via header // field of the original // request. nextHeader = (SIPHeader) ((ViaList) nextHeader).getFirst().clone(); } else { nextHeader = (SIPHeader) nextHeader.clone(); } try { newRequest.attachHeader(nextHeader, false); } catch (SIPDuplicateHeaderException e) { e.printStackTrace(); } } if (MessageFactoryImpl.getDefaultUserAgentHeader() != null) { newRequest.setHeader(MessageFactoryImpl.getDefaultUserAgentHeader()); } return newRequest; }