Esempio n. 1
0
void
sch_output_stream_write_line_2(SchOutputStream *stream, const SchLine *shape, GError **error)
{
    SchOutputStreamPrivate *privat = SCH_OUTPUT_STREAM_GET_PRIVATE(stream);

    if (privat != NULL && shape != NULL)
    {
        gchar *str;
        int   x1;
        int   y1;
        int   x2;
        int   y2;
        int   color;
        int   width;
        int   capstyle;
        int   dashstyle;
        int   dashlength;
        int   dashspace;

        g_object_get(
            (gpointer) shape,
            "x1",               &x1,
            "y1",               &y1,
            "x2",               &x2,
            "y2",               &y2,
            "color",            &color,
            "line-width",       &width,
            "line-cap-style",   &capstyle,
            "line-dash-style",  &dashstyle,
            "line-dash-length", &dashlength,
            "line-dash-space",  &dashspace,
            NULL
            );

        str = g_strdup_printf(
            "%s %d %d %d %d %d %d %d %d %d %d%s",
            "L",
            x1,
            y1,
            x2,
            y2,
            color,
            width,
            capstyle,
            dashstyle,
            dashlength,
            dashspace,
            SCH_OUTPUT_STREAM_EOL
            );

        g_output_stream_write(
            privat->base_stream,
            str,
            strlen(str),
            NULL,
            error
            );

        g_free(str);
    }

}
Esempio n. 2
0
void
sch_output_stream_write_path_2(SchOutputStream *stream, const SchPath *shape, GError **error)
{
    SchOutputStreamPrivate *privat = SCH_OUTPUT_STREAM_GET_PRIVATE(stream);

    if (privat != NULL && shape != NULL)
    {
        gchar *str;
        int   x;
        int   y;
        int   radius;
        int   color;
        int   line_width;
        int   capstyle;
        int   dashstyle;
        int   dashlength;
        int   dashspace;
        int   fill_type;
        int   fill_width;
        int   fill_angle1;
        int   fill_pitch1;
        int   fill_angle2;
        int   fill_pitch2;
        int   count = sch_path_count(shape);

        g_object_get(
            (gpointer) shape,
            "color",            &color,
            "line-width",       &line_width,
            "line-cap-style",   &capstyle,
            "line-dash-style",  &dashstyle,
            "line-dash-length", &dashlength,
            "line-dash-space",  &dashspace,
            "fill-type",        &fill_type,
            "fill-width",       &fill_width,
            "fill-angle1",      &fill_angle1,
            "fill-pitch1",      &fill_pitch1,
            "fill-angle2",      &fill_angle2,
            "fill-pitch2",      &fill_pitch2,
            NULL
            );

        str = g_strdup_printf(
            "%s %d %d %d %d %d %d %d %d %d %d %d %d %d%s",
            "H",
            color,
            line_width,
            capstyle,
            dashstyle,
            dashlength,
            dashspace,
            fill_type,
            fill_width,
            fill_angle1,
            fill_pitch1,
            fill_angle2,
            fill_pitch2,
            count,
            SCH_OUTPUT_STREAM_EOL
            );

        g_output_stream_write(
            privat->base_stream,
            str,
            strlen(str),
            NULL,
            error
            );

        sch_path_foreach(shape, sch_output_stream_write_path_2_proc, stream);

        g_free(str);
    }
}
Esempio n. 3
0
void
sch_output_stream_write_arc_2(SchOutputStream *stream, const SchArc *shape, GError **error)
{
    SchOutputStreamPrivate *privat = SCH_OUTPUT_STREAM_GET_PRIVATE(stream);

    if (privat != NULL && shape != NULL)
    {
        gchar *str;
        int   x;
        int   y;
        int   radius;
        int   start;
        int   sweep;
        int   color;
        int   width;
        int   capstyle;
        int   dashstyle;
        int   dashlength;
        int   dashspace;

        g_object_get(
            (gpointer) shape,
            "center-x",          &x,
            "center-y",          &y,
            "radius",            &radius,
            "start",             &start,
            "sweep",             &sweep,
            "color",             &color,
            "line-width",        &width,
            "line-cap-style",    &capstyle,
            "line-dash-style",   &dashstyle,
            "line-dash-length",  &dashlength,
            "line-dash-space",   &dashspace,
            NULL
            );

        str = g_strdup_printf(
            "%s %d %d %d %d %d %d %d %d %d %d %d%s",
            "A",
            x,
            y,
            radius,
            start,
            sweep,
            color,
            width,
            capstyle,
            dashstyle,
            dashlength,
            dashspace,
            SCH_OUTPUT_STREAM_EOL
            );

        g_output_stream_write(
            privat->base_stream,
            str,
            strlen(str),
            NULL,
            error
            );

        g_free(str);
    }

}
Esempio n. 4
0
void
sch_output_stream_write_box_2(SchOutputStream *stream, const SchBox *shape, GError **error)
{
    SchOutputStreamPrivate *privat = SCH_OUTPUT_STREAM_GET_PRIVATE(stream);

    if (privat != NULL && shape != NULL)
    {
        gchar *str;
        int   x;
        int   y;
        int   width;
        int   height;
        int   color;
        int   line_width;
        int   capstyle;
        int   dashstyle;
        int   dashlength;
        int   dashspace;
        int   fill_type;
        int   fill_width;
        int   fill_angle1;
        int   fill_pitch1;
        int   fill_angle2;
        int   fill_pitch2;

        g_object_get(
            (gpointer) shape,
            "x",                &x,
            "y",                &y,
            "width",            &width,
            "height",           &height,
            "color",            &color,
            "line-width",       &line_width,
            "line-cap-style",   &capstyle,
            "line-dash-style",  &dashstyle,
            "line-dash-length", &dashlength,
            "line-dash-space",  &dashspace,
            "fill-type",        &fill_type,
            "fill-width",       &fill_width,
            "fill-angle1",      &fill_angle1,
            "fill-pitch1",      &fill_pitch1,
            "fill-angle2",      &fill_angle2,
            "fill-pitch2",      &fill_pitch2,
            NULL
            );

        str = g_strdup_printf(
            "%s %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d%s",
            "B",
            x,
            y,
            width,
            height,
            color,
            line_width,
            capstyle,
            dashstyle,
            dashlength,
            dashspace,
            fill_type,
            fill_width,
            fill_angle1,
            fill_pitch1,
            fill_angle2,
            fill_pitch2,
            SCH_OUTPUT_STREAM_EOL
            );

        g_output_stream_write(
            privat->base_stream,
            str,
            strlen(str),
            NULL,
            error
            );

        g_free(str);
    }
}
Esempio n. 5
0
static void
_completion_update_text(GFile *file, const gchar *text)
{
	gchar *path;
	GFileIOStream *file_stream;
	GInputStream *in_stream;
	GOutputStream *out_stream;
	gchar buffer[block_size];
	const gchar *ptr;
	GError *err = NULL;
	gboolean found = FALSE;
	gint32 counter = 0;
	gssize size;

	g_assert(file != NULL);
	g_assert(text != NULL);

	path = g_file_get_path(file);

	g_debug("Opening file (rw): \"%s\"", path);

	if((file_stream = g_file_open_readwrite(file, NULL, &err)))
	{
		/* search for given text */
		g_debug("Searching text...");
		in_stream = g_io_stream_get_input_stream(G_IO_STREAM(file_stream));
		out_stream = g_io_stream_get_output_stream(G_IO_STREAM(file_stream));

		do
		{
			if((size = g_input_stream_read(in_stream, buffer, block_size, NULL, &err)) == block_size)
			{
				ptr = buffer + COMPLETION_COUNTER_SIZE;
				g_debug("Found text: \"%s\"", ptr);

				if(!strcmp(ptr, text))
				{
					found = TRUE;

					/* update counter */
					memcpy(&counter, buffer, COMPLETION_COUNTER_SIZE);

					if(counter != G_MAXINT32)
					{
						counter++;
					}

					g_debug("updating counter: %d", counter);

					if(g_seekable_seek(G_SEEKABLE(out_stream), -block_size, G_SEEK_CUR, NULL, &err))
					{
						g_output_stream_write(out_stream, &counter, COMPLETION_COUNTER_SIZE, NULL, &err);
					}
					else
					{
						g_warning("g_seekable_seek() failed");
					}
				}
			}
			else
			{
				if(size)
				{
					g_warning("Invalid block size: %d", size);
				}
			}
		} while(!err && !found && size);

		if(!found)
		{
			/* append text to file */
			g_debug("Appending new text: \"%s\"", text);

			memset(buffer, 0, block_size);
			memcpy(buffer + COMPLETION_COUNTER_SIZE, text, strlen(text));

			g_output_stream_write(out_stream, buffer, block_size, NULL, &err);
		}

		g_debug("Closing file");
		g_io_stream_close(G_IO_STREAM(file_stream), NULL, NULL);
		g_object_unref(file_stream);
	}
	else
	{
		g_warning("Couldn't open file (rw): \"%s\"", path);
	}

	if(err)
	{
		g_warning("%s", err->message);
		g_error_free(err);
	}

	g_free(path);
}
Esempio n. 6
0
static gboolean
save (GFile *file)
{
  GOutputStream *out;
  GFileCreateFlags flags;
  char buffer[1025];
  char *p;
  gssize res;
  gboolean close_res;
  GError *error;
  gboolean save_res;

  error = NULL;

  flags = priv ? G_FILE_CREATE_PRIVATE : G_FILE_CREATE_NONE;
  flags |= replace_dest ? G_FILE_CREATE_REPLACE_DESTINATION : 0;

  if (create)
    out = (GOutputStream *)g_file_create (file, flags, NULL, &error);
  else if (append)
    out = (GOutputStream *)g_file_append_to (file, flags, NULL, &error);
  else
    out = (GOutputStream *)g_file_replace (file, etag, backup, flags, NULL, &error);
  if (out == NULL)
    {
      g_printerr (_("Error opening file: %s\n"), error->message);
      g_error_free (error);
      return FALSE;
    }

  save_res = TRUE;

  while (1)
    {
      res = read (STDIN_FILENO, buffer, 1024);
      if (res > 0)
	{
	  ssize_t written;

	  p = buffer;
	  while (res > 0)
	    {
	      error = NULL;
	      written = g_output_stream_write (out, p, res, NULL, &error);
	      if (written == -1)
		{
		  save_res = FALSE;
		  g_printerr ("Error writing to stream: %s", error->message);
		  g_error_free (error);
		  goto out;
		}
	      res -= written;
	      p += written;
	    }
	}
      else if (res < 0)
	{
	  save_res = FALSE;
	  perror (_("Error reading stdin"));
	  break;
	}
      else if (res == 0)
	break;
    }

 out:

  close_res = g_output_stream_close (out, NULL, &error);
  if (!close_res)
    {
      save_res = FALSE;
      g_printerr (_("Error closing: %s\n"), error->message);
      g_error_free (error);
    }

  if (close_res && print_etag)
    {
      char *etag;
      etag = g_file_output_stream_get_etag (G_FILE_OUTPUT_STREAM (out));

      if (etag)
	g_print ("Etag: %s\n", etag);
      else
	/* Translators: The "etag" is a token allowing to verify whether a file has been modified */
	g_print (_("Etag not available\n"));
      g_free (etag);
    }

  g_object_unref (out);

  return save_res;
}
Esempio n. 7
0
//Read tcp requests of connected clients
gboolean network_read(GIOChannel *source, GIOCondition cond, gpointer data) {

  GString *s = g_string_new(NULL);
  GError *error = NULL;
  GIOStatus ret = g_io_channel_read_line_string(source, s, NULL, &error);

  if (ret == G_IO_STATUS_ERROR) {
    //unref connection
    GSocketAddress *sockaddr = g_socket_connection_get_remote_address(data, NULL);
    GInetAddress *addr = g_inet_socket_address_get_address(G_INET_SOCKET_ADDRESS(sockaddr));
    //Read sender ip
    if (verbose) {
      printf("App Server: Communication error.. Removing client.. ->%s\n", g_inet_address_to_string(addr));
      fflush(stdout);
    }
    //Remove client
    remove_client(g_inet_address_to_string(addr));
    //Free objects
    g_string_free(s, TRUE);
    g_object_unref(sockaddr);
    g_object_unref(addr);
    g_object_unref(data);
    //Return false to stop listening this socket
    return FALSE;
  }
  else{
    //Read request command
    gchar  *RecString;
    RecString=s->str;
    //check client password
    if ((strncmp(RecString, AppPass, strlen(AppPass))) == 0) {
      //Password ok can send command
      GString *incs = g_string_new(s->str);
      incs = g_string_erase(s,0,(strlen(AppPass)+1));
      IvySendMsg("%s",incs->str);
      if (verbose) {
        printf("App Server: Command passed to ivy: %s\n",incs->str);
        fflush(stdout);
      }
    }
    //AC data request. (Ignore client password)
    else if ((strncmp(RecString, "getac ", strlen("getac "))) == 0) {
      //AC data request
      char AcData[BUFLEN];
      //Read ac data
      if (get_ac_data(RecString, AcData)) {
        //Send requested data to client
        GOutputStream * ostream = g_io_stream_get_output_stream (data);
        g_output_stream_write(ostream, AcData, strlen(AcData), NULL, &error);
      }
    }
    //Waypoint data request (Ignore client password)
    else if ((strncmp(RecString, "getwp ", strlen("getwp "))) == 0) {
      char AcData[BUFLEN];
      //Read wp data of ac
      if (get_wp_data(RecString, AcData)) {
        //Send requested data to client
        GOutputStream * ostream = g_io_stream_get_output_stream (data);
        g_output_stream_write(ostream, AcData, strlen(AcData), NULL, &error);
      }
    }
    //Waypoint data request (Ignore client password)
    else if ((strncmp(RecString, "getbl ", strlen("getbl "))) == 0) {
      char AcData[BUFLEN];
      //Read block data of AC
      if (get_bl_data(RecString, AcData)) {
        //Send requested data to client
        GOutputStream * ostream = g_io_stream_get_output_stream (data);
        g_output_stream_write(ostream, AcData, strlen(AcData), NULL, &error);
      }
    }

    //If client sends removeme command, remove it from broadcast list
    else if ((strncmp( RecString, "removeme", strlen("removeme"))) == 0) {
      GSocketAddress *sockaddr = g_socket_connection_get_remote_address(data, NULL);
      GInetAddress *addr = g_inet_socket_address_get_address(G_INET_SOCKET_ADDRESS(sockaddr));
      //Read sender ip
      if (verbose) {
        printf("App Server: need to remove %s\n", g_inet_address_to_string(addr));
        fflush(stdout);
      }
      //Remove client
      remove_client(g_inet_address_to_string(addr));
      //Free objects
      g_string_free(s, TRUE);
      g_object_unref(sockaddr);
      g_object_unref(addr);
      g_object_unref(data);
      //Return false to stop listening this socket
      return FALSE;
    }
    else {
      //Unknown command
      if (verbose) {
        printf("App Server: Client send an unknown command or wrong password: (%s)\n",RecString);
        fflush(stdout);
      }
    }
  }

  if (ret == G_IO_STATUS_EOF) {
    //Client disconnected
    if (verbose) {
      GSocketAddress *sockaddr = g_socket_connection_get_remote_address(data, NULL);
      GInetAddress *addr = g_inet_socket_address_get_address(G_INET_SOCKET_ADDRESS(sockaddr));
      printf("App Server: Client disconnected without saying 'bye':( ->%s\n", g_inet_address_to_string(addr));
      remove_client(g_inet_address_to_string(addr));
      fflush(stdout);
    }
    g_string_free(s, TRUE);
    //Unref the socket and return false to allow the client to reconnect
    g_object_unref(data);
    return FALSE;
  }
  //None above.. Keep listening the socket
  g_string_free(s, TRUE);
  return TRUE;
}
Esempio n. 8
0
static void
print_uri (gchar *orig, YelpUri *uri, GOutputStream *stream)
{
    GFile *file;
    const gchar *type = NULL;
    gchar *tmp, **tmpv, *out;

    g_output_stream_write (stream, orig, strlen (orig), NULL, NULL);
    g_output_stream_write (stream, "\n", 1, NULL, NULL);

    switch (yelp_uri_get_document_type (uri)) {
    case YELP_URI_DOCUMENT_TYPE_DOCBOOK:
        type = "DOCBOOK";
        break;
    case YELP_URI_DOCUMENT_TYPE_MALLARD:
        type = "MALLARD";
        break;
    case YELP_URI_DOCUMENT_TYPE_MAN:
        type = "MAN";
        break;
    case YELP_URI_DOCUMENT_TYPE_INFO:
        type = "INFO";
        break;
    case YELP_URI_DOCUMENT_TYPE_TEXT:
        type = "TEXT";
        break;
    case YELP_URI_DOCUMENT_TYPE_HTML:
        type = "HTML";
        break;
    case YELP_URI_DOCUMENT_TYPE_XHTML:
        type = "XHTML";
        break;
    case YELP_URI_DOCUMENT_TYPE_HELP_LIST:
        type = "TOC";
        break;
    case YELP_URI_DOCUMENT_TYPE_NOT_FOUND:
        type = "NOT FOUND";
        break;
    case YELP_URI_DOCUMENT_TYPE_EXTERNAL:
        type = "EXTERNAL";
        break;
    case YELP_URI_DOCUMENT_TYPE_ERROR:
        type = "ERROR";
        break;
    case YELP_URI_DOCUMENT_TYPE_UNRESOLVED:
        type = "UNRESOLVED";
        break;
    default:
        g_assert_not_reached ();
        break;
    }

    out = g_strdup_printf ("DOCUMENT TYPE: %s\n", type);
    g_output_stream_write (stream, out, strlen (out), NULL, NULL);
    g_free (out);

    tmp = yelp_uri_get_document_uri (uri);
    if (tmp) {
        out = g_strdup_printf ("DOCUMENT URI:  %s\n", tmp);
        g_output_stream_write (stream, out, strlen (out), NULL, NULL);
        g_free (out);
        g_free (tmp);
    }

    tmp = yelp_uri_get_canonical_uri (uri);
    if (tmp) {
        out = g_strdup_printf ("CANONICAL URI: %s\n", tmp);
        g_output_stream_write (stream, out, strlen (out), NULL, NULL);
        g_free (out);
        g_free (tmp);
    }

    file = yelp_uri_get_file (uri);
    if (file) {
        tmp = g_file_get_uri (file);
        out = g_strdup_printf ("FILE URI:      %s\n", tmp);
        g_output_stream_write (stream, out, strlen (out), NULL, NULL);
        g_free (out);
        g_free (tmp);
        g_object_unref (file);
    }

    tmpv = yelp_uri_get_search_path (uri);
    if (tmpv) {
        int i;
        for (i = 0; tmpv[i]; i++) {
            if (i == 0)
                out = g_strdup_printf ("SEARCH PATH:   %s\n", tmpv[i]);
            else
                out = g_strdup_printf ("               %s\n", tmpv[i]);
            g_output_stream_write (stream, out, strlen (out), NULL, NULL);
            g_free (out);
        }
        g_strfreev (tmpv);
    }

    tmp = yelp_uri_get_page_id (uri);
    if (tmp) {
        out = g_strdup_printf ("PAGE ID:       %s\n", tmp);
        g_output_stream_write (stream, out, strlen (out), NULL, NULL);
        g_free (out);
        g_free (tmp);
    }

    tmp = yelp_uri_get_frag_id (uri);
    if (tmp) {
        out = g_strdup_printf ("FRAG ID:       %s\n", tmp);
        g_output_stream_write (stream, out, strlen (out), NULL, NULL);
        g_free (out);
        g_free (tmp);
    }

    g_output_stream_write (stream, "\0", 1, NULL, NULL);
}
Esempio n. 9
0
int
main (int argc, char *argv[])
{
   /* initialize glib */
  g_type_init ();

  GError * error = NULL;

  /* create a new connection */
  GSocketConnection *X = NULL, *Y = NULL;
  GSocketClient *clientX = g_socket_client_new();
  GSocketClient *clientY = g_socket_client_new();

  /* connect to the host */
  X = g_socket_client_connect_to_host (clientX,
                                       (gchar*)"localhost",
                                       8516, /* your port goes here */
                                       NULL,
                                       &error);
  Y = g_socket_client_connect_to_host (clientY,
                                       (gchar*)"localhost",
                                       8516, /* your port goes here */
                                       NULL,
                                       &error);

  /* use the connection */
  GInputStream * istreamX = g_io_stream_get_input_stream (G_IO_STREAM (X));
  GOutputStream * ostreamX = g_io_stream_get_output_stream (G_IO_STREAM (X));
  GInputStream * istreamY = g_io_stream_get_input_stream (G_IO_STREAM (Y));
  GOutputStream * ostreamY = g_io_stream_get_output_stream (G_IO_STREAM (Y));

  // A CONNECT message that registers this client under the name XXX
  JzMsg *msg1 = jz_msg_new_connect ("XXX", 0);
  GByteArray *arr1 = jz_msg_to_byte_array (msg1);
  g_output_stream_write  (ostreamX,
                          arr1->data,
                          arr1->len, /* length of your message */
                          NULL,
                          &error);

  JzMsg *msg2 = jz_msg_new_connect ("YYY", 0);
  GByteArray *arr2 = jz_msg_to_byte_array (msg2);
  g_output_stream_write  (ostreamY,
                          arr2->data, arr2->len,
                          NULL,
                          &error);

  g_usleep(100000);

  // A CALL request
  GByteArray *empty_data = g_byte_array_new();
  JzMsg *msg3 = jz_msg_new_call_request (1 /* LCN */,
                                         "XXX" /* Calling address */,
                                         "YYY" /* called adress */,
                                         3, // packet facility
                                         3, // window facility
                                         3, // throughput facility
                                         empty_data);

  GByteArray *arr3 = jz_msg_to_byte_array (msg3);
  g_output_stream_write (ostreamX,
                         arr3->data, arr3->len,
                         NULL,
                         &error);

  
  g_usleep(2 * 1000000);
  
  return 0;
}
Esempio n. 10
0
static gboolean
archive_spawnv (GFile                *dir,
                char                **output,
                GError              **error,
                const gchar * const  *argv)
{
  g_autoptr(GSubprocessLauncher) launcher = NULL;
  g_autoptr(GSubprocess) subp = NULL;
  GInputStream *in;
  g_autoptr(GOutputStream) out = NULL;
  g_autoptr(GMainLoop) loop = NULL;
  SpawnData data = {0};
  g_autofree gchar *commandline = NULL;

  launcher = g_subprocess_launcher_new (G_SUBPROCESS_FLAGS_NONE);

  if (output)
    g_subprocess_launcher_set_flags (launcher, G_SUBPROCESS_FLAGS_STDOUT_PIPE);

  if (dir)
    {
      g_autofree char *path = g_file_get_path (dir);
      g_subprocess_launcher_set_cwd (launcher, path);
    }

  commandline = g_strjoinv (" ", (gchar **) argv);
  g_debug ("Running '%s'", commandline);

  subp = g_subprocess_launcher_spawnv (launcher, argv, error);

  if (subp == NULL)
    return FALSE;

  loop = g_main_loop_new (NULL, FALSE);

  data.loop = loop;
  data.refs = 1;

  if (output)
    {
      data.refs++;
      in = g_subprocess_get_stdout_pipe (subp);
      out = g_memory_output_stream_new_resizable ();
      g_output_stream_splice_async (out,
                                    in,
                                    G_OUTPUT_STREAM_SPLICE_NONE,
                                    0,
                                    NULL,
                                    spawn_output_spliced_cb,
                                    &data);
    }

  g_subprocess_wait_async (subp, NULL, spawn_exit_cb, &data);

  g_main_loop_run (loop);

  if (data.error)
    {
      g_propagate_error (error, data.error);
      g_clear_error (&data.splice_error);
      return FALSE;
    }

  if (out)
    {
      if (data.splice_error)
        {
          g_propagate_error (error, data.splice_error);
          return FALSE;
        }

      /* Null terminate */
      g_output_stream_write (out, "\0", 1, NULL, NULL);
      g_output_stream_close (out, NULL, NULL);
      *output = g_memory_output_stream_steal_data (G_MEMORY_OUTPUT_STREAM (out));
    }

  return TRUE;
}
Esempio n. 11
0
static gboolean on_incoming_connection(GThreadedSocketService *service,
    GSocketConnection *connection, GObject *source_object, OwrImageServer *image_server)
{
    GOutputStream *bos;
    GDataInputStream *dis;
    gchar *error_body, *error_header = NULL, *response_header = NULL;
    gchar *line, *tag;
    gsize line_length, i;
    guint content_length = 0;
    OwrImageRenderer *image_renderer;
    GBytes *image;
    gconstpointer image_data;
    gsize image_data_size = 0;

    OWR_UNUSED(service);
    OWR_UNUSED(source_object);

    g_return_val_if_fail(OWR_IS_IMAGE_SERVER(image_server), TRUE);

    bos = g_buffered_output_stream_new(g_io_stream_get_output_stream(G_IO_STREAM(connection)));
    dis = g_data_input_stream_new(g_io_stream_get_input_stream(G_IO_STREAM(connection)));
    g_data_input_stream_set_newline_type(dis, G_DATA_STREAM_NEWLINE_TYPE_CR_LF);

    error_body = "404 Not Found";
    error_header = g_strdup_printf(HTTP_RESPONSE_HEADER_TEMPLATE, 404, "Not Found",
        "text/plain", (guint)strlen(error_body), "*");

    while (TRUE) {
        line = g_data_input_stream_read_line(dis, &line_length, NULL, NULL);
        if (!line)
            break;

        if (line_length > 6) {
            tag = g_strdup(line + 7);
            for (i = 0; i < strlen(tag); i++) {
                if (tag[i] == '-') {
                    tag[i] = '\0';
                    break;
                }
            }
        } else
            tag = NULL;

        g_free(line);

        while ((line = g_data_input_stream_read_line(dis, &line_length, NULL, NULL))) {
            g_free(line);

            if (!line_length) {
                /* got all request headers */
                break;
            }
        }

        if (!line)
            break;

        g_mutex_lock(&image_server->priv->image_renderers_mutex);
        image_renderer = tag ? g_hash_table_lookup(image_server->priv->image_renderers, tag) : NULL;
        if (image_renderer)
            g_object_ref(image_renderer);
        g_mutex_unlock(&image_server->priv->image_renderers_mutex);

        image = image_renderer ? _owr_image_renderer_pull_bmp_image(image_renderer) : NULL;

        if (image_renderer)
            g_object_unref(image_renderer);

        if (!image) {
            g_output_stream_write(bos, error_header, strlen(error_header), NULL, NULL);
            g_output_stream_write(bos, error_body, strlen(error_body), NULL, NULL);
            break;
        }

        image_data = g_bytes_get_data(image, &image_data_size);

        if (content_length != image_data_size) {
            content_length = image_data_size;
            g_free(response_header);
            response_header = g_strdup_printf(HTTP_RESPONSE_HEADER_TEMPLATE, 200, "OK",
                "image/bmp", content_length, image_server->priv->allow_origin);
            g_buffered_output_stream_set_buffer_size(G_BUFFERED_OUTPUT_STREAM(bos),
                strlen(response_header) + content_length);
        }
        g_output_stream_write(bos, response_header, strlen(response_header), NULL, NULL);
        g_output_stream_write(bos, image_data, image_data_size, NULL, NULL);
        g_output_stream_flush(bos, NULL, NULL);

        g_bytes_unref(image);
    }

    g_free(response_header);
    g_free(error_header);
    g_object_unref(dis);
    g_object_unref(bos);

    return FALSE;
}