<commit_before>package net.liftweb.http

/*                                                *\
 (c) 2007 WorldWide Conferencing, LLC
 Distributed under an Apache License
 http://www.apache.org/licenses/LICENSE-2.0
\*                                                 */

import scala.xml.{Node, Unparsed, Group, NodeSeq}
import net.liftweb.util._
import net.liftweb.util.Helpers._
import javax.servlet.http.Cookie
import js._
import java.io.InputStream

trait ToResponse extends LiftResponse {
  def out: Node
  def headers: List[(String, String)]
  def cookies: List[Cookie]
  def code: Int
  def docType: Can[String]

  def toResponse = {
    val encoding =
    (out, headers.ciGet("Content-Type")) match {
    case (up: Unparsed,  _) => ""
    case (_, Empty) | (_, Failure(_, _, _)) => "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
    case (_, Full(s)) if (s.toLowerCase.startsWith("text/xml") ||
                          s.toLowerCase.startsWith("text/html") ||
                          s.toLowerCase.startsWith("text/xhtml") ||
			  s.toLowerCase.startsWith("application/xml") ||
			  s.toLowerCase.startsWith("application/xhtml+xml")) => "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
    case _ => ""
    }

    val doc = docType.map(_ + "\n") openOr ""

    InMemoryResponse((encoding + doc + AltXML.toXML(out, false, false)).getBytes("UTF-8"), headers, cookies, code)
    }
}

trait LiftResponse {
  def toResponse: BasicResponse
}

case class XhtmlResponse(out: Node, docType: Can[String], headers: List[(String, String)],
			 cookies: List[Cookie], code: Int) extends ToResponse

object JsonResponse {
  def apply(json: JsExp): LiftResponse = JsonResponse(json, Nil, Nil, 200)
}

case class JsonResponse(json: JsExp, headers: List[(String, String)], cookies: List[Cookie], code: Int) extends LiftResponse {
	def toResponse = {
		val bytes = json.toJsCmd.getBytes("UTF-8")
		InMemoryResponse(bytes, ("Content-Length", bytes.length.toString) :: ("Content-Type", "application/json") :: headers, cookies, code)<commit_msg>Improved efficiency of converting XML to byte-stream.  Added some trailing whitespace to deal with some clearspace/tomcat wackiness<commit_after>package net.liftweb.http

/*                                                *\
 (c) 2007 WorldWide Conferencing, LLC
 Distributed under an Apache License
 http://www.apache.org/licenses/LICENSE-2.0
\*                                                 */

import scala.xml.{Node, Unparsed, Group, NodeSeq}
import net.liftweb.util._
import net.liftweb.util.Helpers._
import javax.servlet.http.Cookie
import js._
import java.io.InputStream

trait ToResponse extends LiftResponse {
  def out: Node
  def headers: List[(String, String)]
  def cookies: List[Cookie]
  def code: Int
  def docType: Can[String]

  def toResponse = {
    val encoding =
    (out, headers.ciGet("Content-Type")) match {
    case (up: Unparsed,  _) => ""
    case (_, Empty) | (_, Failure(_, _, _)) => "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
    case (_, Full(s)) if (s.toLowerCase.startsWith("text/xml") ||
                          s.toLowerCase.startsWith("text/html") ||
                          s.toLowerCase.startsWith("text/xhtml") ||
			  s.toLowerCase.startsWith("application/xml") ||
			  s.toLowerCase.startsWith("application/xhtml+xml")) => "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
    case _ => ""
    }

    val doc = docType.map(_ + "\n") openOr ""

    val sb = new StringBuilder(64000)
    sb.append(encoding)
    sb.append(doc)
    AltXML.toXML(out, scala.xml.TopScope, sb, false, false)
    sb.append("  \n  ")

    val ret = sb.toString // (encoding + doc + AltXML.toXML(out, false, false))
    
    InMemoryResponse(ret.getBytes("UTF-8"), headers, cookies, code)
    }
}

trait LiftResponse {
  def toResponse: BasicResponse
}

case class XhtmlResponse(out: Node, docType: Can[String], headers: List[(String, String)],
			 cookies: List[Cookie], code: Int) extends ToResponse

object JsonResponse {
  def apply(json: JsExp): LiftResponse = JsonResponse(json, Nil, Nil, 200)
}

case class JsonResponse(json: JsExp, headers: List[(String, String)], cookies: List[Cookie], code: Int) extends LiftResponse {
	def toResponse = {
		val bytes = json.toJsCmd.getBytes("UTF-8")
		InMemoryResponse(bytes, ("Content-Length", bytes.length.toString) :: ("Content-Type", "application/json") :: headers, cookies, code)
{'fraction_non_alphanumeric': 0.14849452554744524, 'fraction_numerical': 0.008895985401459854, 'mean_word_length': 3.0526802218114604, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 4, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 13}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1686484', 'n_tokens_mistral': 1390, 'n_tokens_neox': 1350, 'n_words': 322}